Javascript while循环,其中条件是一个承诺 [英] Javascript while loop where condition is a promise

查看:69
本文介绍了Javascript while循环,其中条件是一个承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在循环中创建本机JavaScript ,其中条件是承诺?

Is it possible to make a native JavaScript while loop where the condition is a promise?

编辑:
我要做的是在将文件上传到firebase存储之前执行检查,以查看具有相同名称的文件存在于firebase存储中。如果已存在具有相同名称的文件,则添加随机后缀并再次检查。

What I'm trying to do is implement a check before uploading a file to firebase storage, to see if a file with the same name already exists in the firebase storage. If there is already a file with the same name then add a random suffix and check again.

var storageRef = firebase.storage().ref().child(fileName);

while(storageRef.getDownloadURL()) {
    // create random number in between 0 and 100
    var random = Math.floor((Math.random() * 100) + 1);    
    storageRef = firebase.storage().ref().child(fileName + random);
}

storageRef.put(file);


推荐答案


是否有可能在条件为承诺的情况下制作原生JavaScript循环吗?

Is it possible to make a native JavaScript while loop where the condition is a promise?

不,它不是。

尝试这样做有几个问题:

There are a couple problems with trying to do that:


  1. 凭借ES6标准承诺,无法直接测试承诺的价值。对最终解析值的唯一访问是使用 .then()。你不能做 while(p!==已解决)

即使你可以循环关于promises值,因为Javascript是事件驱动的和单线程的,如果你做了一个循环,那么promise的异步操作永远不会运行,并且承诺永远不会得到解决。

Even if you could loop on the promises value, because Javascript is event driven and single threaded, if you did do a loop, then the asynchronous operation of the promise could never run and the promise could never get resolved anyway.

相反,你只需使用:

p.then(function(result) {
    // process result here
}).catch(function(err) {
    // process error here
});

如果你想在循环中做更多的事情,你必须在我们之前披露实际的代码可以进一步建议如何最好地做到这一点。

If there's more you wanted to do in your loop, you would have to disclose that actual code before we could advise further on how to best do that.

如果你想重复一些操作直到某些条件,那么就把在函数中执行该操作,在 .then()处理程序中测试条件,如果要重复,则只需再次调用该函数。

If you want to repeat some operation until some condition, then just put that operation in a function, test the condition in the .then() handler and if you want to repeat, then just call the function again.

function doSomething() {
    // some async operation that returns a promise
}

function next() {
    return doSomething.then(function(result) {
        if (result < someValue) {
             // run the operation again
             return next();
        } else {
             return result;
        }
    });
}

next().then(function(result) {
      // process final result here
}).catch(function(err) {
    // process error here
});






编辑:随着ES7,您可以使用 async 等待。它不会是循环,但它将消除对while循环的需要,因为你根本不必轮询异步值。 / p>


With ES7, you can use async and await. It won't really be a while loop, but it will obviate the need for a while loop because you don't have to "poll" the asynchronous value at all.

async function f() {
   let value = await somePromise;
   // put code here to execute after the promise
   console.log(value);
}

函数的内部执行 f()将暂停,直到承诺结算或拒绝。请注意, f()的来电者不会被暂停。 f()将在执行 await 行后立即返回承诺。当 f()的内部执行终于完成时,该承诺本身将解决并拒绝。

The internal execution of function f() will be paused until the promise resolves or rejects. Note that the caller of f() will not be paused. f() will return a promise immediately as soon as the await line is executed. That promise itself will resolve and reject when the internal execution of f() finally finishes.

这篇关于Javascript while循环,其中条件是一个承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆