Asynchronous transactions in javascript

First of all, a rollback is what I don't care about.

I would like to be able to block a sequence of asynchronous functions / promises / tasks (call it a "transaction") with the name / id (or an array of names) so that they happen sequentially, and therefore any other "transaction" with the same name (s), which are executed by another part of the system, is delayed from the beginning to the end of the current transaction using the same name. Thus, it basically performs the sequence of asynchronous tasks or transactions.

Here is a sample code:

function a()
{
  // do stuff
  return new Promise(/*...*/);
}


function b()
{
  // do stuff
  return new Promise(/*...*/);
}



function c()
{
  // do stuff
  return a.then(() => b());
}

a, b c, , , c b, c b.

npm, , , , - -, , ?

+4
6

, gulp . , c b b a

const gulp = require('gulp');
gulp.task('a', done => {
  // do stuff
  console.log('a');
  done();
});

gulp.task('b', ['a'], done => {
  // do stuff
  console.log('b');
  done();
});

gulp.task('c', ['b'], done => {
  // do more stuff
  console.log('c');
  done();
});

gulp.start('c'); // Logs a, b, c

!

+8

async/await babel. Async Babel Docs

function a()
{
  // do stuff
  return new Promise(/*...*/);
}


async function b()
{
  const aData = await a();
  // do stuff
  return new Promise(/*...*/);
}



async function c()
{
  const bData = await b();
  // do stuff
  return bData;
}
+4

.

const transactions = {};

function doTransaction(name, promiseFunc) {
  transactions[name] = (transactions[name] || Promise.resolve()).then(promiseFunc);
}
+3
function a()
{
  // do stuff
  return new Promise(/*...*/);
}

function b()
{
  // do stuff
  return new Promise(/*...*/);
}

function c()
{
  // do stuff
  return new Value;
}

a().then(function(data_a) {
  // you can make use of return value (which is data_a) here or as an argument for function b or even function c
  b().then(function(data_b) {
    // you can make use of return value (which is data_b) here or as an argument for function c
    c().then(function(data_c) {
      // do your coding here
    });
  });
});

: https://spring.io/understanding/javascript-promises

+2

, . b, : doCall wait. .

doCall wait().

wait() , doCall()

, CodePen (. ):

function wrapPromiseFn(fn) {
  var prev = null;
  var doCall = function() {
    var retValue;

    prev = new Promise(function(resolve, reject) {
      retValue = fn();
      retValue.then(function(result) {
        resolve(true);
      });
      retValue.catch(function(result) {
        resolve(true);
      });
    });

    return retValue;
  };

  var wait = function() {
    return prev ? prev : Promise.resolve(false);
  };

  return {
    doCall: doCall,
    wait: wait
  };
}

function a() {
  return Promise.resolve(42);
}

function b() {
  //return Promise.reject(new Date());
  return Promise.resolve(new Date().getTime());
}

var wrappedB = wrapPromiseFn(b);

function doStuff() {
  return wrappedB.wait().then(function(didWait) {
    return a().then(function(_a) {
      return wrappedB.doCall().then(function(_b) {
        console.log("didWait, a, b: ", didWait, _a, _b);
      });
    });
  });
}

//run it twice to prove it did wait
doStuff().then(doStuff)
Hide result

This proves the concept, of course, polishing requires passing arguments from doCallto a wrapped function.

+1
source

Source: https://habr.com/ru/post/1677053/


All Articles