Is it safe to resolve a promise multiple times?

前端 未结 7 858
旧时难觅i
旧时难觅i 2021-01-31 06:33

I have an i18n service in my application which contains the following code:

var i18nService = function() {
  this.ensureLocaleIsLoaded = function() {
    if( !th         


        
7条回答
  •  旧时难觅i
    2021-01-31 07:19

    I faced the same thing a while ago, indeed a promise can be only resolved once, another tries will do nothing (no error, no warning, no then invocation).

    I decided to work it around like this:

    getUsers(users => showThem(users));
    
    getUsers(callback){
        callback(getCachedUsers())
        api.getUsers().then(users => callback(users))
    }
    

    just pass your function as a callback and invoke it as many times you wish! Hope that makes sense.

提交回复
热议问题