From b7b4026fd5b1de0341d686a85f84f5e4d1dba9a7 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Wed, 3 Oct 2018 00:06:16 +0200 Subject: [PATCH] Extended sequence utility no issue - support promise and none promise tasks - helpful if you create an array of operations and not all of the operations/tasks are async - `response instanceof Promise` does not work for all cases e.g. some usages return a transaction/bookshelf chain --- ghost/promise/lib/sequence.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ghost/promise/lib/sequence.js b/ghost/promise/lib/sequence.js index 34d6d42456..c004612008 100644 --- a/ghost/promise/lib/sequence.js +++ b/ghost/promise/lib/sequence.js @@ -1,16 +1,25 @@ -var Promise = require('bluebird'); +const Promise = require('bluebird'); /** * expects an array of functions returning a promise */ function sequence(tasks /* Any Arguments */) { - var args = Array.prototype.slice.call(arguments, 1); + const args = Array.prototype.slice.call(arguments, 1); return Promise.reduce(tasks, function (results, task) { - return task.apply(this, args).then(function (result) { - results.push(result); - return results; - }); + const response = task.apply(this, args); + + if (response && response.then) { + return response.then(function (result) { + results.push(result); + return results; + }); + } else { + return Promise.resolve().then(() => { + results.push(response); + return results; + }); + } }, []); }