How to sequentially run promises with Q in Javascript?
but instead they are being output immediately for some reason.
You are calling them already here:
promiseFuncs.push(getDelayedString(str));
// ^^^^^
You would need to push function(){ return getDelayedString(str); }
. Btw, instead of using pushing to an array in an each
loop you rather should use map
. And actually you don't really need that anyway but can reduce
over the strings
array directly:
function onceUponATime() {
var strings = ["Once", "upon", "a", "time"];
return strings.reduce(function (soFar, s) {
return soFar.then(function() {
return getDelayedString(s);
});
}, Q());
}
Oh, and don't use document.write
.