[Pas2js] sequencing function calls in pas2js
warleyalex
warleyalex at yahoo.com.br
Fri Jun 8 23:47:23 CEST 2018
I read through various threads and it really escapes me how to accomplish the
following in pas2js:
-------------------------------
setTimeout(function() {
console.log('hi after 10 secs')
setTimeout(function() {
console.log('hi after 5 more secs')
setTimeout(function() {
console.log('hi after 2 more secs')
}, 2000)
}, 5000)
}, 10000)
-------------------------------------------------------
wait 10 sec then print the msg "hi, after 10 secs"
wait more 5 sec then print the msg "hi after 5 more secs"
wait more 2 sec then print the msg "hi after 2 more secs"
-------------------------------------------------------
My working solution now looks like this:
==========================================
procedure ProcessMessage;
function _doTask0(aResponse: JSValue): JSValue;
begin
result := wait(10000);
end;
function _doTask1(aResponse: JSValue): JSValue;
begin
console.log('hi, after 10 secs');
result := wait(5000);
end;
function _doTask2(aResponse: JSValue): JSValue;
begin
console.log('hi after 5 more secs');
result := wait(2000);
end;
function _doTask3(aResponse: JSValue): JSValue;
begin
console.log('hi after 2 more secs');
result := wait(0);
end;
begin
queue
.&then(@_doTask0)
.&then(@_doTask1)
.&then(@_doTask2)
.&then(@_doTask3);
end;
==================================================
I ended up to implement the "queue" promise function. Indeed, I just define
a simple external variable:
var queue: JPromise; external name 'Promise.resolve()';
I use this "queue" method (to chaining methods) a lot, I consider the code
readable and maintainable, but I think it could be highly flexible,
something like ("pass additional parameters to then chain").
I read through various pascal threads, but it really escapes me how to
accomplish this in pas2js world, the following:
queue
.&then( @_doTask0('10000') )
.&then (@_doTask1('5000') )
.&then( @_doTask2('2000') )
.&then( @_doTask3('0') );
I tried to figure out how to pass directly a function name with a argument
to another function but I am getting lost :( The method would be highly
flexible if pointer could accept arguments.
______________________________
OBS: In JS world, we can use Function.prototype.bind to create a new
function with a value passed to its first argument, like this
.then(_doTask0.bind(null, '10000'))
.then(_doTask1.bind(null, '5000'))
.then(_doTask2.bind(null, '2000'))
.then(_doTask3.bind(null, '0'));
______________________________
Isn't there some simple way to do this?
--
Sent from: http://pas2js.38893.n8.nabble.com/
More information about the Pas2js
mailing list