如何在Javascript中用Q顺序运行promises?

Nic*_*ick 8 javascript promise q

我很难顺序运行promises.

var getDelayedString = function(string) {
    var deferred = Q.defer();

    setTimeout(function() {
        document.write(string+" ");
        deferred.resolve();
    }, 500);

    return deferred.promise;
};

var onceUponATime = function() {
    var strings = ["Once", "upon", "a", "time"];

    var promiseFuncs = [];

    strings.forEach(function(str) {
        promiseFuncs.push(getDelayedString(str));
    });

    //return promiseFuncs.reduce(Q.when, Q());
    return promiseFuncs.reduce(function (soFar, f) {
        return soFar.then(f);
    }, Q());    
};

getDelayedString("Hello")
.then(function() {
    return getDelayedString("world!")
})
.then(function() {
    return onceUponATime();
})
.then(function() {
    return getDelayedString("there was a guy and then he fell.")
})
.then(function() {
    return getDelayedString("The End!")
})
Run Code Online (Sandbox Code Playgroud)

onceUponATime()应该按顺序输出["Once","on","a","time"],但是由于某种原因它们会立即输出.

jsFiddle这里:http://jsfiddle.net/6Du42/2/

知道我做错了什么吗?

Ber*_*rgi 15

但相反,由于某种原因,它们会立即输出.

你在这里打电话给他们:

promiseFuncs.push(getDelayedString(str));
//                                ^^^^^
Run Code Online (Sandbox Code Playgroud)

你需要推动function(){ return getDelayedString(str); }.顺便说一句,而不是使用推送到each循环中的数组,你应该使用map.实际上你并不是真的需要它,但可以直接reducestrings阵列上:

function onceUponATime() {
    var strings = ["Once", "upon", "a", "time"];

    return strings.reduce(function (soFar, s) {
        return soFar.then(function() {
            return getDelayedString(s);
        });
    }, Q());    
}
Run Code Online (Sandbox Code Playgroud)

哦,不要用document.write.