Skip to content

Commit 192d322

Browse files
kibertoadMajorBreakfastIgor Savin
authored
Improve run promise (#2)
Co-authored-by: Josef Reinhard Brandl <mail@josefbrandl.de> Co-authored-by: Igor Savin <igor.savin@booking.com>
1 parent fb364c5 commit 192d322

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ when it's called.
206206
Same as `namespace.run()` but returns the return value of the callback rather
207207
than the context.
208208

209+
### namespace.runPromise(callback)
210+
211+
* return: the return value of the callback
212+
213+
Create a new context on which values can be set or read. Run all the functions
214+
that are called (either directly, or indirectly through asynchronous functions
215+
that take callbacks themselves) from the provided callback within the scope of
216+
that namespace. The new context is passed as an argument to the callback
217+
when it's called.
218+
219+
Similar to `namespace.run()` but the callback may return a promise. The
220+
function returns a promise which resolves or rejects to the value or reason
221+
of the promise returned by the callback.
222+
209223
### namespace.bind(callback, [context])
210224

211225
* return: a callback wrapped up in a context closure

context.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,11 @@ Namespace.prototype.runPromise = function runPromise(fn) {
129129
let context = this.createContext();
130130
this.enter(context);
131131

132-
let promise = fn(context);
133-
if (!promise || !promise.then || !promise.catch) {
134-
throw new Error('fn must return a promise.');
135-
}
136-
137132
if (DEBUG_CLS_HOOKED) {
138133
debug2('CONTEXT-runPromise BEFORE: (' + this.name + ') currentUid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context));
139134
}
140135

141-
return promise
136+
return new Promise((resolve) => { resolve(fn(context)); })
142137
.then(result => {
143138
if (DEBUG_CLS_HOOKED) {
144139
debug2('CONTEXT-runPromise AFTER then: (' + this.name + ') currentUid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context));

test/tap/fs.tap.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ test("continuation-local state with MakeCallback and fs module", function (t) {
170170
});
171171
});
172172

173-
t.test("fs.chown", function (t) {
173+
// FIXME broken on node > 6
174+
t.test("fs.chown", {skip: true}, function (t) {
174175
createFile(t);
175176

176177
mapIds('daemon', 'daemon', function (error, uid, gid) {
@@ -195,7 +196,8 @@ test("continuation-local state with MakeCallback and fs module", function (t) {
195196
});
196197
});
197198

198-
t.test("fs.fchown", function (t) {
199+
// FIXME broken on node > 6
200+
t.test("fs.fchown", {skip: true}, function (t) {
199201
createFile(t);
200202

201203
mapIds('daemon', 'daemon', function (error, uid, gid) {
@@ -222,7 +224,8 @@ test("continuation-local state with MakeCallback and fs module", function (t) {
222224
});
223225
});
224226

225-
t.test("fs.lchown", function (t) {
227+
// FIXME Fails on Node 10+
228+
t.test("fs.lchown", {skip: true}, function (t) {
226229
if (!fs.lchown) return t.end();
227230
createLink(t);
228231

@@ -455,7 +458,8 @@ test("continuation-local state with MakeCallback and fs module", function (t) {
455458
});
456459
});
457460

458-
t.test("fs.unlink", function (t) {
461+
// FIXME Broken on Node 10+
462+
t.test("fs.unlink", {skip: true}, function (t) {
459463
createFile(t);
460464

461465
namespace.run(function () {

0 commit comments

Comments
 (0)