私はそれがその関数の内部で起こっているのフェッチに待つ必要があるので、私はfindUserInApiを約束したいルートこの機能を約束するにはどうすればよいですか?
app.get('/api/users/:username', (req, res) => {
let username = req.params.username;
findUserInApi(username)
.then(foundUser => {
if (foundUser) {
res.status(403).send('User not found in api yet');
}else{
res.json(foundUser);
}
});
});
へ急行使用しています。
これは、彼が見つかったときに見つかったかfoundUserないとき、私はちょうどヌルを返すために使用されるが、私はPromise.resolveを返すことで、それらのうちの約束を作ってみましたfindUserInApi
const findUserInApi = (username) => {
findChattersPerRole()
.then(chattersPerRole => {
console.log(`${username} should\'ve been added by now...`);
//console.log(chattersPerRole);
let wantedUser = find(chattersPerRole, {username});
if (!wantedUser) {
console.log(`${wantedUser} wasn't found m8`);
return Promise.resolve(null);
}
console.log('wanteduser is: ', wantedUser);
getUserByUsername(wantedUser.username)
.then(foundUser => {
console.log('founduser is: ', foundUser);
return Promise.resolve(foundUser);
});
});
};
です。
const findUserInApi = (username) => {
return findChattersPerRole()
.then(chattersPerRole => {
console.log(`${username} should\'ve been added by now...`);
//console.log(chattersPerRole);
let wantedUser = find(chattersPerRole, {username});
if (!wantedUser) {
console.log(`${wantedUser} wasn't found m8`);
return Promise.resolve(null);
}
console.log('wanteduser is: ', wantedUser);
getUserByUsername(wantedUser.username)
.then(foundUser => {
console.log('founduser is: ', foundUser);
return Promise.resolve(foundUser);
});
});
};
注return findChattersPerRole().then()
でreturn
の追加:私は...あなたはこのように、あなたの全体の約束チェーンを返す必要が
'findUserInApi'は未定義を返します。代わりに約束を返すだけですか? – Oriol
'return findChattersPerRole()'? 'then'関数はそれ自身の約束を返しますので、' Promise.resolve'を呼び出すことによって 'resolve promise'を返す必要があるかどうかはわかりません。 –