te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to resolve a promise multiple times? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to resolve a promise multiple times? - Stack Overflow

programmeradmin3浏览0评论

It might sound weird, but I'm looking for a way to resolve a promise multiple times. Are there any approaches to make this possible?

Think of the following example:

getPromise() {
  const event = new Event('myEvent');

  setTimeout(() => {
    window.dispatchEvent(event);
  }, 5000);

  setTimeout(() => {
    window.dispatchEvent(event);
  }, 7000);

  return new Promise((resolve) => {
    window.addEventListener('myEvent', () => {
      resolve('some value'));
    });

    resolve('some value'));
  });
};

And then .then():

getPromise().then(data => {console.log(data)})

Should give the following result:

some value // initial
some value // after 5000ms
some value // after 7000ms

So I know there are libraries to stream data, but I'm really looking for a native non-callbak approach to achieve this.

It might sound weird, but I'm looking for a way to resolve a promise multiple times. Are there any approaches to make this possible?

Think of the following example:

getPromise() {
  const event = new Event('myEvent');

  setTimeout(() => {
    window.dispatchEvent(event);
  }, 5000);

  setTimeout(() => {
    window.dispatchEvent(event);
  }, 7000);

  return new Promise((resolve) => {
    window.addEventListener('myEvent', () => {
      resolve('some value'));
    });

    resolve('some value'));
  });
};

And then .then():

getPromise().then(data => {console.log(data)})

Should give the following result:

some value // initial
some value // after 5000ms
some value // after 7000ms

So I know there are libraries to stream data, but I'm really looking for a native non-callbak approach to achieve this.

Share Improve this question asked Sep 26, 2017 at 1:22 OrlandsterOrlandster 4,8584 gold badges34 silver badges45 bronze badges 1
  • 3 Promises cannot be resolved multiple times. – Felix Kling Commented Sep 26, 2017 at 1:23
Add a ment  | 

2 Answers 2

Reset to default 13

How to resolve a promise multiple times?

You can't. Promises can only be resolved once. Once they have been resolved, they never ever change their state again. They are essentially one-way state machines with three possible states pending, fulfilled and rejected. Once they've gone from pending to fulfilled or from pending to rejected, they cannot be changed.

So, you pretty much cannot and should not be using promises for something that you want to occur multiple times. Event listeners or observers are a much better match than promises for something like that. Your promise will only ever notify you about the first event it receives.

I don't know why you're trying to avoid callbacks in this case. Promises use callbacks too in their .then() handlers. You will need a callback somewhere to make your solution work. Can you explain why you don't just use window.addEventListener('myEvent', someCallback) directly since that will do what you want?


You could return a promise-like interface (that does not follow Promise standards) that does call its notification callbacks more than once. To avoid confusion with promises, I would not use .then() as the method name:

function getNotifier() {
  const event = new Event('myEvent');

  setTimeout(() => {
    window.dispatchEvent(event);
  }, 500);

  setTimeout(() => {
    window.dispatchEvent(event);
  }, 700);

  let callbackList = [];
  const notifier = {
      notify: function(fn) {
          callbackList.push(fn);
      }
  };
  window.addEventListener('myEvent', (data) => {
      // call all registered callbacks
      for (let cb of callbackList) {
          cb(data);
      }
  });
  return notifier;
};

// Usage:
getNotifier().notify(data => {console.log(data.type)})

I have a solution in Typescript.

export class PromiseParty {
  private promise: Promise<string>;
  private resolver: (value?: string | PromiseLike<string>) => void;
  public getPromise(): Promise<string> {
     if (!this.promise) {
        this.promise = new Promise((newResolver) => { this.resolver = newResolver; });
     }
        return this.promise;
   }
   public setPromise(value: string) {
      if(this.resolver) {
         this.resolver(value);
         this.promise = null;
         this.resolver = null;
      }
   }
}

export class UseThePromise {
   public constructor(
       private promiseParty: PromiseParty
   ){
      this.init();
   }
   private async init(){
      const subscribe = () => {
         const result = await this.promiseParty.getPromise();
         console.log(result);
         subscribe(); //resubscribe!!
      }
      subscribe(); //To start the subscribe the first time
   }  
}


export class FeedThePromise {
   public constructor(
      private promiseParty: PromiseParty
   ){
        setTimeout(() => {
           this.promiseParty.setPromise("Hello");
        }, 1000);
        setTimeout(() => {
           this.promiseParty.setPromise("Hello again!");
        }, 2000);

        setTimeout(() => {
           this.promiseParty.setPromise("Hello again and again!");
        }, 3000);
    }
 }
发布评论

评论列表(0)

  1. 暂无评论