我一直在玩弄异步生成器,试图创建一个“承诺排序”生成器,该生成器接受一系列承诺,并按照它们解析或拒绝的顺序逐一生成承诺。所以,就像:
async function* orderProms(prom_arr) {
// Make a copy so the splices don't mess it up.
const proms = [...prom_arr];
while (proms.length) {
// Tag each promise with it's index, so that we can remove it fo
条件
first two async functions start at the same time
don't know which one finish first
在完成这两个操作后,需要执行第三个函数。
尝试使用promise。请使用promise和其他方式来帮助实现它,如果可能的话。
一些我试过但不确定是否正确的事情。
function foop(a){
return new Promise(function(resolve, reject){
window.setTimeout(function(){
console.log("first promi
以下是我对当前情况的理解:
for-循环中的异步/等待应该暂停执行,直到承诺得到解决,这意味着在该行完成之前,循环的下一次迭代不会发生。
考虑以下数组:
const data = [];
for (let i = 0; i <= 100000; i++) {
data.push(i);
}
方法1:在回循环中等待承诺
async function method1() {
const startTime = new Date();
console.log('start:', startTime);
for (const item in data) {
对不起我的英语不好!
我有这样的代码结构:
var fooResolved;
int();
setTimeout(() => fooResolved(), 3000); // Just for testing we resolve the foo using a timeout manually
async function int(){
await foo('usage');
console.log('foo Resolved'); // if we see this log the foo is resolved
}
//