From 3e8be64fd7e92fdab03d5428de594c2dbca3bd99 Mon Sep 17 00:00:00 2001 From: Fadhli Dzil Ikram Date: Tue, 22 Nov 2016 11:32:49 +0700 Subject: [PATCH] Disable auto-resolve on return keyword Disabling auto-resolve functionality to auto resolve variable that passed in return keyword. This behavior could impact negatively on intentional non-resolving promise or generator return (e.g. returning Promises that needs to be resolved later on) --- lib/setlist.js | 20 +++++--------------- package.json | 2 +- test/setlist.js | 12 ------------ 3 files changed, 6 insertions(+), 28 deletions(-) diff --git a/lib/setlist.js b/lib/setlist.js index 81d1edc..660acee 100644 --- a/lib/setlist.js +++ b/lib/setlist.js @@ -53,23 +53,13 @@ function run(fn) { // Generator next function function next(r) { - let cont - let fail if (r.done) { - cont = resolve - fail = reject + resolve(r.value) } else { - cont = exec - fail = error - } - - if (isPromise(r.value)) { - r.value.then(cont).catch (fail) - } else if (isGenerator(r.value)) { - run(r.value).then(cont).catch (fail) - } else { - if (r.done) { - resolve(r.value) + if (isPromise(r.value)) { + r.value.then(exec).catch(error) + } else if (isGenerator(r.value)) { + run(r.value).then(exec).catch(error) } else { process.nextTick(() => exec(r.value)) } diff --git a/package.json b/package.json index 09d442b..78e49cd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "setlist", - "version": "0.2.2", + "version": "0.2.3", "description": "Sequential-ish your asynchronous code with ES6 Generator Function and Promise", "main": "index.js", "scripts": { diff --git a/test/setlist.js b/test/setlist.js index d47b1b7..3d1d814 100644 --- a/test/setlist.js +++ b/test/setlist.js @@ -65,26 +65,14 @@ describe('Generator runner sanity checking test', function () { describe('Generator runner functional test', function () { it('Non-async return', function () { - let gf = function* (v) { return v } - return $(gf(true)).should.fulfilledWith(true) - }) - it('Non-async yield return', function () { let gf = function* (v) { return yield v } return $(gf(true)).should.fulfilledWith(true) }) it('Promise return', function () { - let gf = function* (v) { return Promise.resolve(v) } - return $(gf(true)).should.fulfilledWith(true) - }) - it('Promise yield return', function () { let gf = function* (v) { return yield Promise.resolve(v) } return $(gf(true)).should.fulfilledWith(true) }) it('Generator function return', function () { - let gf = function* (v) { return function* (v) { return v }(v) } - return $(gf(true)).should.fulfilledWith(true) - }) - it('Generator function yield return', function () { let gf = function* (v) { return yield function* (v) { return v }(v) } return $(gf(true)).should.fulfilledWith(true) })