Working with promises inside an if/else

后端 未结 10 1082
北恋
北恋 2021-02-02 10:46

I have a conditional statement in which I need to perform one of two operations, then continue after whichever operation has resolved. So my code currently looks as follows:

10条回答
  •  忘了有多久
    2021-02-02 11:26

    Simple working example:

    The scope it's defined in must be async.

    const createUser = async (data) => {
       if (await isUsernameTaken(username)) { return 'username-taken' }
    }
    

    The isUsernameTaken func:

    const isUsernameTaken = async (username) => {
        const request = await API.SomeRequest
        return !request.isEmpty
    }
    

提交回复
热议问题