How to solve the problem that promise is executed only once?

here is a simple promise call

new Promise(function(success,){
    setTimeout(function(){success()},1000);
}).then(function(){
    console.log(1)
});

as you can see, the console can output 1

The then method of

Promise can do callback, but what if this is the case?

function test(back){
    setInterval(function(){back()},1000)
}
test(function(){
    console.log(1);
});

/ / you can keep outputting 1

.

and if you can"t do it with promise:

new Promise(function(success,){
    setInterval(function(){success()},1000);
}).then(function(){
    console.log(1)
});

does Promise have hidden advanced features in addition to then,catch?

Jul.23,2021

the subject does not give an application scenario, so briefly explain my understanding (also the rabbit stage).
first of all, I think that the subject has a little extreme view of Promise, and not all cases need to use Promise's then, chained call to solve the problem of asynchronous readability and maintainability. In the current situation, the subject can return an immediately executed fn (that is, without the then keyword).
secondly, Promise will have some other keywords, such as all , join , map (this uncertainty), and so on, which are suitable for different request scenarios. Finally, the above is just a personal opinion, which may be due to misunderstandings and deviations due to learning limitations.


Promise can only be executed once, which will not change
, but can be encapsulated according to different business
repeated calls. You can redeclare one Promise at a time to call

.
function p(){
  return new Promise(resolve =>{
    setTimeout(resolve,1000)
  }).then(()=>{
    console.log(1)
  })
}

setInterval(p,1000)

I suggest you learn more about Promise first.
Promise itself has only three states, pending,resolved and rejected, and the state can only be transformed from pending to the other two, and cannot be reset, that is, a promise can only transform the state once, so using only one Promise can not meet your requirements. Create one after each execution can meet your requirements.

const cb = () => console.log(1)
const handler = () => new Promise(resolve => {
        setTimeout(() => {
            cb()
            resolve()
        }, 1000)
    }).then(handler)
handler()
Menu