Immediately return a resolved promise using AngularJS

后端 未结 5 1102
故里飘歌
故里飘歌 2020-12-10 00:57

I\'m trying to get my head around promises in JavaScript (in particular AngularJS).

I have a function in a service, let\'s call it fooService, that chec

5条回答
  •  一向
    一向 (楼主)
    2020-12-10 01:28

    Similar to Elo's answer, you can return an already resolved promise using the async/await syntax:

    this.update = async (data_loaded) => {
    
        if (data_loaded) 
            return await null;  // Instead of null, you could also return something else
                                // like a string "Resolved" or an object { status: 200 }
        else 
            return await OtherPromise();
    }
    

提交回复
热议问题