From 48e62b44b569a252f4f0f12c7abda386a5cc4f36 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sat, 27 Aug 2016 23:58:43 +0300 Subject: [PATCH] Do not wait on promises if caller sets a callback explicitly --- index.js | 7 +++++-- package.json | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 625e1f5..435d356 100644 --- a/index.js +++ b/index.js @@ -107,9 +107,10 @@ function callGen(thread, method, arg) } else if (v.done && thread._onsuccess) thread._onsuccess(v.value); - else if (typeof v.value == 'object' && v.value.then) + else if (typeof v.value == 'object' && v.value.then && !thread._current) { // check if v.value is a Promise + // (but not if an explicit .then(gen.cb()) callback is already set by caller) v.value.then(function(value) { // use process.nextTick so Promise does not intercept our exceptions @@ -141,6 +142,7 @@ function threadCallback() getStack(fn)+'\n--' ); } + thread._current = null; return callGen(thread, 'next', Array.prototype.slice.call(arguments, 0)); }; fn._stack = new Error().stack; @@ -161,12 +163,13 @@ function errorFirst() getStack(fn)+'\n--' ); } + thread._current = null; if (arguments[0]) { var e = arguments[0]; var m = /^([\s\S]*?)((\n\s*at.*)*)$/.exec(e.stack); if (m) - e.stack = m[1]+getStack(thread._current)+'\n-- async error thrown at:'+m[2]; + e.stack = m[1]+getStack(fn)+'\n-- async error thrown at:'+m[2]; return callGen(thread, 'throw', e); } return callGen(thread, 'next', Array.prototype.slice.call(arguments, 1)); diff --git a/package.json b/package.json index 407836c..e9d40d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gen-thread", - "version": "1.0.4", + "version": "1.0.5", "author": { "name": "Vitaliy Filippov", "email": "vitalif@yourcmc.ru"