2017-07-18 88 views

回答

1

它可以返回任何東西,因爲

let x = await doSomething() 
// ... rest of your code 

// is roughly equivalent 

Promise 
    .resolve(doSomething()) 
    .then(value => { 
    let x = value 

    // ... rest of your code 
    }) 

但如果doSomething是異步它返回的東西 「thenable」,使await可能實際工作

const doSomething =() => ({ 
 
    then(fn) { 
 
    setTimeout(fn, 2000, 'hello') 
 
    } 
 
}) 
 

 
const run = async() => { 
 
    let msg = await doSomething() 
 
    
 
    console.log(msg) 
 
} 
 

 
run().then(() => console.log('Done'))