0
我目前有一個項目,它使用本地承諾。我期待將這些Promise遷移到Async/Await。我有麻煩遷移他們;我正在嘗試關注this文章。以下是Promise需要更改爲Async/Await的當前代碼。Node.JS - 承諾異步/等待示例
routes.js
// Importing user information
import user from '../server-controllers/user';
// User Information Route
router.get('/about', (req, res) => {
user.profile().then((data) => {
return res.render('user', {
title: data,
});
}).catch((e) => {
res.status(500, {
error: e,
});
});
});
user.js的
/*
This file contains any server side modules needed.
*/
module.exports = {
// Returns information about a user
profile:() => {
return new Promise((resolve, reject) => {
const user = "John Doe";
resolve(user);
});
},
};
如果有什麼我需要做的這些轉換,這將是有幫助的任何幫助。我不知道代碼是否需要更改routes
或user
文件(或兩者)。
,我在我的終端正的錯誤是[object Error] { ... }
這裏的整個應用程序與異步/ AWAIT:https://開頭github.com/bryanmacfarlane/sanenode – bryanmac
只需用'await'替換每個'then'調用即可。 – Bergi