如何进入 [英] How to access the

查看:92
本文介绍了如何进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是编写 callOneByOne 函数,除一件小事外,我已完成所有工作.我想得到

The objective is to write the callOneByOne function, and I have it all done I hope except for one tiny thing. I want to get the

推荐答案

这是您无需访问参数即可解决的一个谜.从 printCall 返回的函数已经在闭包中包含了 waitingTime .技巧是按正确的顺序安排回调,以便以嵌套方式调用它们.

This is a riddle that you are supposed to solve without needing access to the arguments. The returned function from printCall already has waitingTime in a closure. The trick is to arrange the callbacks in the correct order so they get called in a nested fashion.

这是一种方法:

'use strict';

function printCall(waitingTime) {

  return function(callback) {

    setTimeout(() => {
      console.log(`${waitingTime}ms have passed`);
      if (callback) {
        callback();
      }
    }, waitingTime);

  }
}

function callOneByOne($$, $) {
  // pretty huh?
  return  _ => $$(() => $(_))
}

let calls = [printCall(3000), printCall(2000), printCall(4000)];

(calls.reduce((__, _) => callOneByOne(__, _), _ => _()))();

这篇关于如何进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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