Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态创建承诺_Javascript_Dynamic_Promise_Es6 Promise - Fatal编程技术网

Javascript 动态创建承诺

Javascript 动态创建承诺,javascript,dynamic,promise,es6-promise,Javascript,Dynamic,Promise,Es6 Promise,我有一个相当简单的问题,我似乎无法解决。我在google/stackoverflow上也找不到任何东西(也许我只是使用了错误的关键字?) 我有一个值数组,我想为数组中的每个元素调用某个函数。棘手的是,函数返回一个承诺,并且只有在该承诺得到解决后才应该再次调用 如果我这样做,我就不会在下一次函数调用之前等待承诺得到解决: let paramerterArr = ['a','b','c','d','e','f'] paramerterArr.forEach((currentParam) =>

我有一个相当简单的问题,我似乎无法解决。我在google/stackoverflow上也找不到任何东西(也许我只是使用了错误的关键字?)

我有一个值数组,我想为数组中的每个元素调用某个函数。棘手的是,函数返回一个承诺,并且只有在该承诺得到解决后才应该再次调用

如果我这样做,我就不会在下一次函数调用之前等待承诺得到解决:

let paramerterArr = ['a','b','c','d','e','f']
paramerterArr.forEach((currentParam) => {
    let promise = mySpecialFunction(currentParam)
})
如果我这样做,我将不得不编写大量冗余代码,而且我不能仅仅更改阵列:

let paramerterArr = ['a','b','c','d','e','f']
mySpecialFunction(paramerterArr[0]).then(()=> {
    return mySpecialFunction(paramerterArr[1])
}).then(()=> {
    return mySpecialFunction(paramerterArr[2])
}).then(()=> {
    return mySpecialFunction(paramerterArr[3])
}).then(()=> {
    return mySpecialFunction(paramerterArr[4])
}).then(()=> {
    return mySpecialFunction(paramerterArr[5])
})
let paramerterArr = ['a','b','c','d','e','f']
let currentPos = 0
mySpecialFunction(currentPos).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
})
即使我这样做,我也不能改变阵列:

如果我这样做,我将不得不编写大量冗余代码,而且我不能仅仅更改阵列:

let paramerterArr = ['a','b','c','d','e','f']
mySpecialFunction(paramerterArr[0]).then(()=> {
    return mySpecialFunction(paramerterArr[1])
}).then(()=> {
    return mySpecialFunction(paramerterArr[2])
}).then(()=> {
    return mySpecialFunction(paramerterArr[3])
}).then(()=> {
    return mySpecialFunction(paramerterArr[4])
}).then(()=> {
    return mySpecialFunction(paramerterArr[5])
})
let paramerterArr = ['a','b','c','d','e','f']
let currentPos = 0
mySpecialFunction(currentPos).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
}).then(()=> {
    currentPos++
    return mySpecialFunction(currentPos)
})
我就是想不出一个聪明的方法来做…

也许你们中的某个人有一个想法,那就太好了。

如果你想在系列中实现你的承诺,你可以使用Array.reduce()


什么是
Q()
。我使用的是原生JS承诺(ECMA6)。我可以用Promise()或其他类似的方法来代替吗?如果这个解决方案对你有效,你能接受这个答案吗?谢谢当然我只是想在做这件事之前确保它能正常工作。看这里-