Promise(resolve, reject) on Flutter

When we use async function, we want to trigger resolve without calculation cost.
That is why we do not use computed waiting like sleep.
In Node.js, we usually use Promise(resolve, reject).

In these days, I use flutter. and I develop my own throttle and debounce.
For these async controller needs async management.

At the first I choose whiled sleep and go. but It has critical-section problem for duration control.

Example Javascript promise

async function invoke(targetFunctionCall) {
  var asyncPromiseHandle = Promise(resolve, reject) {
    try {
      var rv = await targetFunctionCall();
      
      resolve(rv);
    } catch (err) {
      reject(err);
    }
  }

  return asyncPromiseHandle;
}

In Flutter, that gives completer function for this.

Promise(resolve, reject) for Flutter

Here is code of Flutter

Future invoke(Function targetFunctionCall) async {
  var completer = Completer();
  
  try {
    var rv = await targetFunctionCall();
    
    completer.complete(rv);
  } catch (err, stack) {
    completer.completeError(err, stack);
  }

  return completer.future;
}