How to create a function that returns an existing promise instead of new promise?

后端 未结 2 1466
刺人心
刺人心 2020-12-12 02:34

JavaScript/Promise experts,

I hope you can help me, because I don\'t understand how I can create a function that returns an existing promise instead of a new promise

2条回答
  •  醉酒成梦
    2020-12-12 02:39

    Try the new variable approach suggested. Promises are designed to be single-shot, so it depends on the implementation. What you are trying to do is more in the arena of events or reactivex. If you are using jquery then you can use their event scheme. A cleaner one is available on the nodejs site. Since it does not require anything else it runs in the browser. rxjs is the cream of the stream processing crop, but it is a big library and requires some learning. Worth it - since the same knowledge is useful client and server in many languages. You can even set up a stream from a promise - among many other ways.

提交回复
热议问题