无法修改功能以打印其他功能的内容 [英] Can't modify a function to print the content of another function

查看:59
本文介绍了无法修改功能以打印其他功能的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 node 节点中编写了一个脚本,使用了三个提供回调的函数。

I've written a script in node using three different functions supplying callback within them.

如何修改 printResult()函数,使设计保持原样为了在 addGender()函数中打印内容?

How can I modify the printResult() function keeping the design as it is in order to print the content within addGender() function?

这是什么我写过:

const name = "sam"

function getName(callback) {
  callback(name);
}

function addTitle(name,callback) {
  var title = "writer";
  callback(name,title);
}

function addGender(name,title,callback) {
  var gender = "male";
  console.log({name,title,gender});
}

printResult();


推荐答案

为什么 addGender ,则有回调

const name = "sam"

function getName(callback) {
  callback(name);
}

function addTitle(name,callback) {
  var title = "writer";
  callback(name,title);
}

function addGender(name,title,callback) {
  var gender = "male";
  console.log({name,title,gender});
}

function printResult() {
  getName(function(name) {
    addTitle(name, function(name, title) {
      addGender(name, title)
    })
  })
}

printResult();

此处是其中 addGender 使用回调

Here is a modified version where addGender uses a callback

const name = "sam"

function getName(callback) {
  callback(name);
}

function addTitle(name,callback) {
  var title = "writer";
  callback(name,title);
}

function addGender(name,title,callback) {
  var gender = "male";
  callback({name,title,gender})
}

function printResult() {
  getName(function(name) {
    addTitle(name, function(name, title) {
      addGender(name, title, function(result) {
        console.log(result)
      })
    })
  })
}

printResult();

这篇关于无法修改功能以打印其他功能的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆