const rp = require('request-promise')
async function main () {
const result = await rp('https://google.com')
const twenty = await 20
// 睡个1秒钟
await new Promise (resolve => {
setTimeout(resolve, 1000)
})
return result
}
main()
.then(console.log)
.catch(console.error)
const util = require('util')
const {readFile} = require('fs')
const readFileAsync = util.promisify(readFile)
async function main () {
const result = await readFileAsync('.gitignore')
return result
}
main()
.then(console.log)
.catch(console.error)
const express = require('express')
const app = express()
app.get('/', async (request, response) => {
// 在这里等待 Promise
// 如果你只是在等待一个单独的 Promise,你其实可以直接将将它作为返回值返回,不需要使用 await 去等待。
const result = await getContent()
response.send(result)
})
app.listen(process.env.PORT)
const awaitHandlerFactory = (middleware) => {
return async (req, res, next) => {
try {
await middleware(req, res, next)
} catch (err) {
next(err)
}
}
}
// 然后这样使用:
app.get('/', awaitHandlerFactory(async (request, response) => {
const result = await getContent()
response.send(result)
}))
async function main () {
const user = await Users.fetch(userId)
const product = await Products.fetch(productId)
await makePurchase(user, product)
}
async function main () {
const [user, product] = await Promise.all([
Users.fetch(userId),
Products.fetch(productId)
])
await makePurchase(user, product)
}
async function main () {
await new Promise((resolve, reject) => {
reject(new Error('error'))
})
}
main()
.then(console.log)
const util = require('util')
async function main () {
try {
await new Promise((resolve, reject) => {
reject(new Error('💥'))
})
} catch (err) {
// 在这里处理错误
// 根据你的需要,有时候把错误直接再抛出也是可行的
}
}
main()
.then(console.log)
.catch(console.error)
mapLimit, filterLimit, concatLimit,
const util = require('util')
const async = require('async')
const numbers = [
1, 2, 3, 4, 5
]
mapLimitAsync = util.promisify(async.mapLimit)
async function main () {
return await mapLimitAsync(numbers, 2, (number, done) => {
setTimeout(function () {
done(null, number * 2)
}, 100)
})
}
main()
.then(console.log)
.catch(console.error)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有