A small problem of sequential execution of asynchronous functions and array processing

topic description

two asynchronous functions are required to write a function that asyncOneByOne (arr) can sequentially execute input parameters

asyncOneByOne([one, two])
// 
//first
//second

function one(callback) {
    setTimeout(function () {
        console.log("first");
        callback && callback();
    }, 200);
}

function two(callback) {
    setTimeout(function () {
        console.log("second");
        callback && callback();
    }, 0);
}

sources of topics and their own ideas

A written test question, at first I thought I would use promise or something, but one () two () didn"t let me change
, and then I found that as long as
one (two (one (two)
, I could execute it (read the callback function chapter)

.

what result do you expect? What is the error message actually seen?

so the problem becomes the problem of entering an array [one, two, one, two], how to change it into one (two (one (two) and how to execute it, but here it has been pasted out. Let me ask you how to deal with it.

Jan.21,2022

function asyncOneByOne(params) {
  if (params.length) {
    params[0](() => {
      asyncOneByOne(params.splice(1));
    });
  }
}
asyncOneByOne([one, two, one, two, two])

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    asyncOneByOne([one, two,one,two]);
    // 
    //first
    //second
    function asyncOneByOne(arr){
        var len=arr.length;
        var s=arr[len-1];
        for(var i=len-2;i>=0;i--){
            s=(function(x){
                var m=s;
                return function(){
                    arr[x](m);
                }
            })(i)
        }
        s();
    }
    function one(callback) {
        setTimeout(function () {
            console.log('first');
            callback && callback();
        }, 200);
    }

    function two(callback) {
        setTimeout(function () {
            console.log('second');
            callback && callback();
        }, 0);
    }
</script>
</body>
</html>

you can also use Promise

function asyncOneByOne(logArr){
    function doNext(lo,hi){
        if(loPP>=hi)return
        new Promise((res,rej)=>logArr[lo](res))
        .then(()=>doNext(lo,hi))
    }
    doNext(-1,logArr.length-1)
}

asyncOneByOne([one,two,one,two])
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb8457-26bd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb8457-26bd.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?