2016-11-12 5 views
1

私はそれがその関数の内部で起こっているのフェッチに待つ必要があるので、私は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の追加:私は...あなたはこのように、あなたの全体の約束チェーンを返す必要が

+0

'findUserInApi'は未定義を返します。代わりに約束を返すだけですか? – Oriol

+2

'return findChattersPerRole()'? 'then'関数はそれ自身の約束を返しますので、' Promise.resolve'を呼び出すことによって 'resolve promise'を返す必要があるかどうかはわかりません。 –

答えて

2

これは固定取得するかどうかはわかりません。

しかし、あなたはプロミスチェーンをネストしています。それは反パターンです。また、コメントに記載されているように、.then()Promise.resolve()を使用する必要はなく、自動的に適用されます。あなたのコードは次のように再構成できます:

const findUserInApi = (username) => { 
    return findChattersPerRole() 
    .then(chattersPerRole => { 
     console.log(`${username} should\'ve been added by now...`); 
     //console.log(chattersPerRole); 
     return find(chattersPerRole, {username}); 
    }) 
    .then(wantedUser => { 
     if (wantedUser) { 
     console.log('wanteduser is: ', wantedUser); 
     return getUserByUsername(wantedUser.username); 
     } 
     else console.log(`${wantedUser} wasn't found m8`); 
    }) 
    .then(foundUser => { 
     if (foundUser) console.log('founduser is: ', foundUser); 
     return foundUser; 
    }); 
}; 
+0

ああ、私はそれを見始めていると思う。ですから、もしあなたが約束を始めて、次の「その時」に何かを返せば、その約束を守ることができますか?あなたはそれぞれの "その時"に約束を返す必要はありませんか? – Kevin

+0

"それぞれの約束を返す必要はありません"、 "?"約束や価値を返すことができます。 'then'の値を返すと、' then'はその値で解決された約束を返します。 「約束を守るだけでいいの?」正しい。それが約束の力です! – RyanZim

関連する問題