const fs = require('fs') function readFile(fileName) { return new Promise((resolve, reject) => { fs.readFile(fileName, (err, data) => { if(err) { reject(err) } resolve(data.toString()) }) }) }
readFile('data/a.txt').then(res => console.log(res)) .catch(err => console.log(err)) readFile('data/b.txt').then(res => console.log(res)) .catch(err => console.log(err)) readFile('data/c.txt').then(res => console.log(res)) .catch(err => console.log(err))
function* gen() { yield readFile('data/a.txt') yield readFile('data/b.txt') yield readFile('data/c.txt') } let it = gen() it.next().value.then(res => { console.log(res) }).catch(err => { console.log(err) }) it.next().value.then(res => { console.log(res) }).catch(err => { console.log(err) }) it.next().value.then(res => { console.log(res) }).catch(err => { console.log(err) })
async function read() { let readA = await readFile('data/a.txt') let readB = await readFile('data/b.txt') let readC = await readFile('data/c.txt') console.log(readA) console.log(readB) console.log(readC) } read()
最終的輸出結果html
// 函數聲明 async function foo() { // .... } // 函數表達式 let foo = async function() { // .... } // 箭頭函數 let foo = async() => {} // 對象的方法 let obj = { name: 'Roger', async foo() { } } obj.foo().then(res => { // .... }) // 類的方法 class Student{ constructor(name, age) { this.name = name this.age = age } async say() { return `My name is ${this.name}, I'm ${this.age} years old !` } } let jim = new Student('Jim Green', 13) jim.say().then(res => console.log(res)) // My name is Jim Green, I'm 13 years old !
// 休眠 ms 毫秒 function sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms) }) } async function print(ms) { console.log('start... ...') await sleep(ms) console.log('end... ...') } print(1000)
async function foo() { return 'hello world' } foo().then(res => console.log(res)) // hello world
async function bar() { return new Error('Error... ...') } bar().then(res => console.log(res)) .catch(err => console.log(err)) // Error: Error... ...
async function baz() { await new Promise(resolve => { console.log('執行第一個異步操做') setTimeout(resolve, 2000) }) await new Promise(resolve => { console.log('執行第二個異步操做') setTimeout(resolve, 3000) }) return '異步執行完畢再執行then方法' } baz().then(res => {console.log(res)})
[return_value] = await expression 表達式:一個 Promise對象或者任何要等待的值 返回值:返回 Promise對象的處理結果。若是等待的不是 Promise對象,則返回該值自己
// 若是 await 命令後的表達式的值不是一個 Promise,則返回該值自己 async function foo() { return await 123 } foo().then(res => console.log(res)) // 123 // 若是 Promise 正常處理(fulfilled),其回調的resolve函數參數做爲 await 表達式的返回值 async function bar() { let f = await new Promise((resolve, reject) => { resolve('我是表達式的返回值') }) console.log(f) // 我是表達式的返回值 return 'ending' } bar().then(res => {console.log(res)}) // ending // 若是 Promise 處理異常(rejected),await 表達式會把 Promise 的異常緣由拋出 async function baz() { await new Promise((resolve, reject) => { reject(new Error('出錯啦......')) }) } baz().then(res => console.log(res)) .catch(err => console.log(err)) // Error: 出錯啦......
async function foo() { await Promise.reject('error') return 'ending' // 未執行 } foo().then(res => console.log(res)) // Uncaught (in promise) error
async function foo() { try{ await Promise.reject('error') } catch(e) { } return await Promise.resolve('執行完畢') } foo().then(res => console.log(res)) // 執行完畢
async function foo() { await Promise.reject('error').catch(err => console.log(err)) return '執行完畢' } foo().then(res => console.log(res)) // 執行完畢
// 寫法一 let [foo, bar] = await Promise.all([getFoo(), getBar()]); // 寫法二 let fooPromise = getFoo(); let barPromise = getBar(); let foo = await fooPromise; let bar = await barPromise;
原文出處:https://www.cnblogs.com/rogerwu/p/10784236.htmlexpress