Javascript 将其余代码作为回调函数传递

Javascript 将其余代码作为回调函数传递,javascript,Javascript,在已经完成的javascript函数中间,您需要调用一个新的异步函数。所以您需要将代码的其余部分作为回调函数作为参数传递给这个新函数 function sample() { alert("a bunch of codes"); alert("another a bunch of codes"); } 我必须改变功能如下 function sample() { alert("a bunch of codes"); var cb = function () {

在已经完成的javascript函数中间,您需要调用一个新的异步函数。所以您需要将代码的其余部分作为回调函数作为参数传递给这个新函数

function sample() {
    alert("a bunch of codes");

    alert("another a bunch of codes");
}
我必须改变功能如下

function sample() {
    alert("a bunch of codes");

    var cb = function () {
        alert("another a bunch of codes");
    };

    newFunction(cb);
}
如果我想添加另一个必须等待第一个的函数,该怎么办?然后我得到了大量的多级回调函数来等待另一个


那么ES5的最佳实践是什么呢?

在ES5中,正如您所说,您必须在彼此内部嵌套多个回调

例如:

function myFunction2(){
    console.log(2);

  let myFunction = () => {
    console.log(1);
    }

  myFunction();
}
myFunction2();
// OUTPUT
// 2
// 1
let myPromise = new Promise((resolve, reject) => {
  setTimeout(function(){
    resolve(1);
  }, 250);
});

console.log(2);
myPromise.then((successMessage) => {
  console.log(successMessage);
});
// OUTPUT
// 2
// 1
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}


async function add1(x) {
  const a = await resolveAfter2Seconds(20);
  const b = await resolveAfter2Seconds(30);
  return x + a + b;
}

add1(10).then(v => {
  console.log(v);  // prints 60 after 4 seconds.
});
ES6还提供了一种新的替代方案

例如:

function myFunction2(){
    console.log(2);

  let myFunction = () => {
    console.log(1);
    }

  myFunction();
}
myFunction2();
// OUTPUT
// 2
// 1
let myPromise = new Promise((resolve, reject) => {
  setTimeout(function(){
    resolve(1);
  }, 250);
});

console.log(2);
myPromise.then((successMessage) => {
  console.log(successMessage);
});
// OUTPUT
// 2
// 1
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}


async function add1(x) {
  const a = await resolveAfter2Seconds(20);
  const b = await resolveAfter2Seconds(30);
  return x + a + b;
}

add1(10).then(v => {
  console.log(v);  // prints 60 after 4 seconds.
});
ES8提供了一个更好的替代方案,尽管它只是基于承诺的语法糖,但您可以将函数与wait一起使用

例如:

function myFunction2(){
    console.log(2);

  let myFunction = () => {
    console.log(1);
    }

  myFunction();
}
myFunction2();
// OUTPUT
// 2
// 1
let myPromise = new Promise((resolve, reject) => {
  setTimeout(function(){
    resolve(1);
  }, 250);
});

console.log(2);
myPromise.then((successMessage) => {
  console.log(successMessage);
});
// OUTPUT
// 2
// 1
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}


async function add1(x) {
  const a = await resolveAfter2Seconds(20);
  const b = await resolveAfter2Seconds(30);
  return x + a + b;
}

add1(10).then(v => {
  console.log(v);  // prints 60 after 4 seconds.
});

但请记住,为了兼容所有浏览器,您可能需要使用Babel来传输js。

谢谢您的回答,我知道ES5之后会有一些解决方案,但我需要在ES5上有一个棘手或智能的解决方案。@KnowGe我相信除了ES5中的回调地狱之外,没有其他解决方法,这就是后来添加这些功能以解决此问题的原因。