简体   繁体   English

用回调编写函数

[英]Writing a function with a callback

Lets imagine that I have some code: 假设我有一些代码:

var someString = "";
function do1(){
    doA();
    doB();
}

function doA(){
    // some process that takes time and gets me a value
    someString = // the value I got in prior line

function doB(){
    //do something with someString;
}

What is the correct way to make sure somestring is defined by doB tries to use it? 确保doB定义somestring尝试使用它的正确方法是什么? I think this is a situation that calls for a callback, but I'm not sure how to set it up? 我认为这种情况需要回调,但是我不确定如何设置它?

Usually, I have solved this problem like following code by callback parameter. 通常,我已经解决了这个问题,例如通过回调参数遵循以下代码。 However, I don't know this is correct answer. 但是,我不知道这是正确的答案。 In my case, it's done well. 就我而言,做得很好。

var someString = "";
function do1(){ 
    doA(doB);
}

function doA(callback){
    // some process that takes time and gets me a value
    someString = // the value I got in prior line
    callback();
}

function doB(){
    //do something with someString;
}

I usually write these such that the function can be called with, or without, the callback function. 我通常编写这些代码,以便可以在有或没有回调函数的情况下调用该函数。 You can do this by calling the callback function only if typeof callback === 'function' . 仅当typeof callback === 'function'才可以通过调用callback函数来实现此目的。 This allows the function which includes the possibility of a callback to be a bit more general purpose. 这使得包括回调可能性的功能更加通用。 The call to the callback() , obviously, needs to be from within the callback of whatever asynchronous operation you are performing. 显然,对callback()的调用需要来自您正在执行的任何异步操作的回调。 In the example below, setTimeout is used as the asynchronous action. 在下面的示例中, setTimeout用作异步操作。

 var someString = ""; function do1() { doA(doB); //Call doA with doB as a callback. } function doA(callback) { setTimeout(function() { //setTimeout is an example of some asynchronous process that takes time //Change someString to a value which we "received" in this asynchronous call. someString = 'Timeout expired'; //Do other processing that is normal for doA here. //Call the callback function, if one was passed to this function if (typeof callback === 'function') { callback(); } }, 2000); } function doB() { //do something with someString; console.log(someString); } do1(); 

You can, of course, do this without using a global variable: 您当然可以在不使用全局变量的情况下执行此操作:

 function do1() { doA(doB); //Call doA with doB as a callback. } function doA(callback) { setTimeout(function() { //setTimeout is an example of some asynchronous process that takes time //Simulate a result var result = 'Timeout expired'; //Do other processing that is normal for doA here. //Call the callback function, if one was passed to this function if (typeof callback === 'function') { callback(result); } }, 2000); } function doB(result) { console.log(result); } do1(); 

function someFunctionA(callback){
  var someString = "modify me";
  callback(someString);
}

function someFunctionB(someString){
  // do something
}

function main() {
   someFunctionA(somefunctionB);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM