0

From my understanding, await makes the entire function wait until the statement it precedes finishes, right? I'm not sure why the below code prints this:

RES: [object Promise]
cn: 你好wurld!
mg: வணக்கம் wurld!
xh: Molo wurld!
FINAL: Url hello!

Here's my code:

const rp = require('request-promise')
const apiKey = //commented out for obvious reasons lol

const muddle = async(toTranslate)=>{
  let options = {
    method: 'GET',
    json: true,
    uri: `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${toTranslate}&lang=en-zh`
  }
  let response = await rp(options)
  let newText = response.text[0]
  console.log('cn: ' + newText)

  options.uri =`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${toTranslate}&lang=zh-ta`
  response = await rp(options)
  newText = response.text[0]
  console.log('mg: ' +newText)

  options.uri =`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${toTranslate}&lang=ta-xh`
  response = await rp(options)
  newText = response.text[0]
  console.log('xh: ' +newText)

  options.uri = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${newText}&lang=xh-en`
  response = await rp(options)
  newText = response.text[0]
  console.log('FINAL: ' + newText)

  return response.text[0] //why does this fire before all the lines that include await before it?
}


let toLog = muddle('Hello wurld!')
console.log('RES: '+ toLog)

Shouldn't RES: [object Promise] be the line that prints last? Sorry for the massive wall of code, but it's pretty redundant for the most part. Each block is just a request to a translate API, which translates the translation returned from the request preceding it. Am I misunderstanding how await works? I want to return the final string that's been run through translate four times ('Url hello!)

4

1 回答 1

1

RES 没有最后打印是正常的,因为您在记录之前没有等待 mudle 函数完成。

你有两个选择来解决这个问题:

使用then语法:

muddle('Hello wurld!').then(toLog => {
  console.log('RES: '+ toLog)
})

将您的调用包装在另一个异步函数中:

async function main() {
  const toLog = await muddle('Hello wurld!')
  console.log('RES: '+ toLog)
}
main();

不过要小心第二个:你可能会得到同步代码。更容易处理但可能效率低下

于 2019-08-02T15:45:08.303 回答