admin管理员组文章数量:1405379
I've created a very short/simple example of a problem I'm having with Promise
s in a script utilizing the aws-sdk
package for Node.js.
In short, the script is not waiting at the await
keyword. It loops through the loop without waiting for the async
function to successfully plete before moving on.
Code example:
main.js
const AWS = require('aws-sdk')
import constants from '@/constants'
AWS.config.update({
accessKeyId: constants.awsAccessKey,
secretAccessKey: constants.awsSecretAccessKey,
region: constants.awsRegion
})
export const s3 = new AWS.S3({apiVersion: '2006-03-01'})
click some button and trigger testS3()
method...
testActions.js
import { s3 } from '@/main.js'
export async function testS3 () {
const testParams = {
Bucket: AWS_BUCKET,
Key: `test_file.txt`,
Body: 'Testing stuff'
}
async function testFunction(layer) {
console.log('in prepare design 1')
const result = await s3.putObject(testParams).promise()
console.log(`the results: ${result}`)
}
[1,2,3].forEach(async (x) => {
const result = await testFunction()
})
}
Output from the debugger:
I was expecting the messages to be interleaved, which is what makes sense if you follow the flow of the logic.
It should be in prepare design 1
, and then display the results...
, then go through and do that 2 more times.
The output displaying in prepare design 1
three times right off the bat shows me that the loop is not waiting for the function before continuing.
Have I setup async
/await
wrong in some way? I've tried multiple different iterations and can't seem to get this working how I'm expecting it to.
I've created a very short/simple example of a problem I'm having with Promise
s in a script utilizing the aws-sdk
package for Node.js.
In short, the script is not waiting at the await
keyword. It loops through the loop without waiting for the async
function to successfully plete before moving on.
Code example:
main.js
const AWS = require('aws-sdk')
import constants from '@/constants'
AWS.config.update({
accessKeyId: constants.awsAccessKey,
secretAccessKey: constants.awsSecretAccessKey,
region: constants.awsRegion
})
export const s3 = new AWS.S3({apiVersion: '2006-03-01'})
click some button and trigger testS3()
method...
testActions.js
import { s3 } from '@/main.js'
export async function testS3 () {
const testParams = {
Bucket: AWS_BUCKET,
Key: `test_file.txt`,
Body: 'Testing stuff'
}
async function testFunction(layer) {
console.log('in prepare design 1')
const result = await s3.putObject(testParams).promise()
console.log(`the results: ${result}`)
}
[1,2,3].forEach(async (x) => {
const result = await testFunction()
})
}
Output from the debugger:
I was expecting the messages to be interleaved, which is what makes sense if you follow the flow of the logic.
It should be in prepare design 1
, and then display the results...
, then go through and do that 2 more times.
The output displaying in prepare design 1
three times right off the bat shows me that the loop is not waiting for the function before continuing.
Have I setup async
/await
wrong in some way? I've tried multiple different iterations and can't seem to get this working how I'm expecting it to.
2 Answers
Reset to default 4The displayed behavior is correct, because forEach
's handler is also an async
function:
async (x) => {
const result = await testFunction()
}
So the forEach
loop will run 3 async functions immediately. Each of those function will await
for the rest of the promise chain asynchronously.
If you want a synchronous execution, use a normal for loop:
for(var i=0; i<3; i++){
const result = await testFunction()
}
This an addition to AVAVT's answer (as I can't ment on it), you can also do so as such so you don't have to manually enter in the number of iterations and intending on using the iterating value.
import { s3 } from '@/main.js'
export async function testS3 () {
const testParams = {
Bucket: AWS_BUCKET,
Key: `test_file.txt`,
Body: 'Testing stuff'
}
async function testFunction(layer) {
console.log('in prepare design 1')
const result = await s3.putObject(testParams).promise()
console.log(`the results: ${result}`)
}
for (const iterator of [1,2,3]) {
const result = await testFunction();
}
}
本文标签: javascriptAsyncawait Not working with AWS S3 Nodejs SDKStack Overflow
版权声明:本文标题:javascript - Asyncawait Not working with AWS S3 Node.js SDK - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744274942a2598364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论