javascript - Put a promise in a pending state -
i want defer execution of promise, until user input provided.
i want post, wait until has happened, this:
while (true) { promiseforuserinput = pending; strinput = rl.question("provide input"); //waits user input promiseforuserinput = resolved; var resultofuserschoice = promiseforuserinput.then(function(input) { return loadfromserver(input) }).then(function(serverresponse) { console.log(serverresponse) }); }
is possible? need read on factory functions?
that have work this:
new promise(function (resolve) { document.getelementbyid('my-input').addeventlistener('keyup', function (e) { if (/* input value enough */) { resolve(e.currenttarget.value); } }); })
to input (i'm assuming browser context here), have bind event listener input element , read input element upon decided event. inside promise, , resolve
promise you're happy input value.
you can .then
promise:
thepromise.then(function (input) { alert(input); });
the input data resolved value.
Comments
Post a Comment