Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/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_Node.js_Promise_Async Await - Fatal编程技术网

Javascript &引用;等待仅在异步函数中有效;内循环

Javascript &引用;等待仅在异步函数中有效;内循环,javascript,node.js,promise,async-await,Javascript,Node.js,Promise,Async Await,我被告知“wait仅在异步函数中有效”,即使它在异步函数中也是如此。这是我的密码: async function uploadMultipleFiles (storageFilePaths,packFilePaths,packRoot) { return new Promise((resolve,reject) => { try { for (i in storageFilePaths) { await upl

我被告知“wait仅在异步函数中有效”,即使它在异步函数中也是如此。这是我的密码:

async function uploadMultipleFiles (storageFilePaths,packFilePaths,packRoot) {
    return new Promise((resolve,reject) => {
        try {
            for (i in storageFilePaths) {
                await uploadFile(storageFilePaths[i],packFilePaths[i],packRoot) // error throws on this line
            }
            resolve("files uploaded")
        } catch {
            console.log(err)
            reject("fail")
        }
    })
}

当我将它设置为异步函数时,为什么会发生这种情况?是因为我使用了for循环吗?如果是这样,如何在没有此错误的情况下获得预期结果?

从第1行开始定义的函数是
async

您在第2行定义并传递给Promise构造函数的箭头函数是异步的


您还使用了多承诺反模式。完全摆脱承诺构造器。只要在有值时返回它。这是
async
关键字的主要优点之一

async function uploadMultipleFiles(storageFilePaths, packFilePaths, packRoot) {
    try {
        for (i in storageFilePaths) {
            await uploadFile(storageFilePaths[i], packFilePaths[i], packRoot) // error throws on this line
        }
        return "files uploaded";
    } catch {
        console.log(err);
        throw "fail";
    }
}

从第1行开始定义的函数是
async

您在第2行定义并传递给Promise构造函数的箭头函数是异步的


您还使用了多承诺反模式。完全摆脱承诺构造器。只要在有值时返回它。这是
async
关键字的主要优点之一

async function uploadMultipleFiles(storageFilePaths, packFilePaths, packRoot) {
    try {
        for (i in storageFilePaths) {
            await uploadFile(storageFilePaths[i], packFilePaths[i], packRoot) // error throws on this line
        }
        return "files uploaded";
    } catch {
        console.log(err);
        throw "fail";
    }
}

您只能在
函数内部使用
,错误是指您传递给新的
函数的回调(因为您正在那里输入新的函数范围)


您只能在
函数内部使用
,错误是指您传递给新的
函数的回调(因为您正在那里输入新的函数范围)


Promise
回调不是异步的

async function uploadMultipleFiles (storageFilePaths,packFilePaths,packRoot) {
    return new Promise(async (resolve,reject) => {
        try {
            for (i in storageFilePaths) {
                await uploadFile(storageFilePaths[i],packFilePaths[i],packRoot) // error throws on this line
            }
            resolve("files uploaded")
        } catch {
            console.log(err)
            reject("fail")
        }
    })
}

Promise
回调不是异步的

async function uploadMultipleFiles (storageFilePaths,packFilePaths,packRoot) {
    return new Promise(async (resolve,reject) => {
        try {
            for (i in storageFilePaths) {
                await uploadFile(storageFilePaths[i],packFilePaths[i],packRoot) // error throws on this line
            }
            resolve("files uploaded")
        } catch {
            console.log(err)
            reject("fail")
        }
    })
}

此处的相关函数是您为承诺声明的lambda,而不是UploadMultipleFile。此处的相关函数是您为承诺声明的lambda,不上传多文件。回调到
new Promise
异步函数是确定的。回调到
new Promise
异步函数是确定的。当我在代码后面调用
uploadMultipleFiles
时,它在异步函数中,被称为
等待上传多文件
。如果我放弃了这里的承诺,我怎么能在以后等待它呢?@pofferfish-
async
函数总是返回承诺当我在代码的后面调用
uploadMultipleFiles
时,它在一个异步函数中,被称为
await uploadMultipleFiles
。如果我去掉这里的承诺,我以后怎么能等待它呢?@pufferfish-
async
函数总是返回承诺
拒绝(“失败”)
返回“失败”
是不等价的。一个
拒绝
和一个
抛出
是等效的。@t.niese true,resolve('fail')在我的情况下是等效的,很好。我将编辑我的回答,只是一些挑剔的
拒绝(“失败”)
抛出新错误(“失败”)也不是等效的<代码>拒绝(“失败”)
抛出“失败”是或
拒绝(新错误(“失败”)
抛出新错误(“失败”)
。但是我同意在这两种情况下都应该使用
错误
(或从中继承的类)。@t.niese同意,只是一般不喜欢抛出字符串,因为它可能会导致此处讨论的一些问题:。新的错误部分只是我的建议,但实际上并不完全等同于原始帖子中显示的示例;)我同意这一点,但如果重构现有代码,它应该是等效的。因为您永远不知道被拒绝的值是否用作某个字符串。但是可以肯定的是,
新错误
更好。
拒绝(“失败”)
返回“失败”
并不等同。一个
拒绝
和一个
抛出
是等效的。@t.niese true,resolve('fail')在我的情况下是等效的,很好。我将编辑我的回答,只是一些挑剔的
拒绝(“失败”)
抛出新错误(“失败”)也不是等效的<代码>拒绝(“失败”)
抛出“失败”是或
拒绝(新错误(“失败”)
抛出新错误(“失败”)
。但是我同意在这两种情况下都应该使用
错误
(或从中继承的类)。@t.niese同意,只是一般不喜欢抛出字符串,因为它可能会导致此处讨论的一些问题:。新的错误部分只是我的建议,但实际上并不完全等同于原始帖子中显示的示例;)我同意这一点,但如果重构现有代码,它应该是等效的。因为您永远不知道被拒绝的值是否用作某个字符串。但是可以肯定的是,
新错误
更好。