# fed-e-task-01-01 **Repository Path**: hanfengmi/fed-e-task-01-01 ## Basic Information - **Project Name**: fed-e-task-01-01 - **Description**: test - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-18 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # fed-e-task-01-01 ### 简答一、谈谈你是如何理解JS异步编程的,EventLoop、消息队列都是做什么的,什么是宏任务,什么是微任务 > JS是单线程的,程序的执行是同步的,上一个任务执行完成才会执行下一个任务,为了提高代码执行效率,我们往往使用异步编程的方式来执行任务,以提高代码的运行效率,但是异步任务无法确定何时能结束,所以需要使用回调函数来处理异步任务的结果。 **同步任务**:进入主线程开始排队顺序执行 **异步任务**:不进入主线程而进入任务队列,等待可以执行时再进入主线程执行 **任务队列**就是**消息队列**只要异步任务有了运行结果就在消息队列中放置一个事件,一旦同步任务执行完毕,系统就会读取消息队列,开始执行异步任务的回调。消息队列是先进先出的结构,排在前面的事件优先执行。 > **事件循环(EventLoop)**:同步任务在主线程上完成,异步任务有了结果在消息队列中添加事件,主线程中同步任务执行完毕,读取消息队列中的事件,进入(主线程)执行栈,开始执行,上述过程是循环不断执行的就形成了事件循环。 > 当前执行栈执行完毕时会立刻先处理所有微任务队列中的事件,然后再去宏任务队列中取第一个事件。同一次事件循环中,微任务永远在宏任务之前执行。 > 宏任务 marcotask(tasks),包括 > * setTimeout > * setInterval > * requestAnimationFrame > * UI rendering > * setImmediate(Node) > * I/O >微任务microtask(jobs),包括 > * Promise > * Object.observe > * MutationObserver > * process.nextTick (Node) ### 代码一、将下面异步代码使用Promise的方式改进 **答案见 code/test/index1.js** ``` setTimeout(function(){ var a = 'hello' setTimeout(function(){ var b = 'lagou' setTimeout(function(){ var c = 'I 💗 U' console.log(a + b + c) },10) },10) },10) ``` ### 代码二、基于以下代码完成下面四个练习 **答案见 code/test/index2.js** ``` cosnt fp = require('lodash/fp') // 数据 // horsepower 马力, dollar_value 价格, in_stock 库存 const cars = [ {name:'Ferrari FF',horsepower:660,dollar_value:700000,in_stock:true}, {name:'Spyker C12 Zagato',horsepower:650,dollar_value:648000,in_stock:false}, {name:'Jaguar XKR-S',horsepower:550,dollar_value:132000,in_stock:false}, {name:'Audi R8',horsepower:525,dollar_value:114200,in_stock:false}, {name:'Aston Martin One-77',horsepower:750,dollar_value:185000,in_stock:true}, {name:'Pagani Huayra',horsepower:700,dollar_value:130000,in_stock:false}, ] ``` #### 1:使用函数fp.flowRight重新实现下面函数 ``` let isLastInStock = function(cars){ // 获取最后一条数据 let last_car = fp.last(cars) // 获取最后一条数据的in_stock 属性值 return fp.prop('in_stock',last_car) } ``` #### 2:使用函数fp.flowRight()、fp.prop()、fp.first()获取第一个car的name #### 3:使用帮助函数_average重构averageDollarValue,使用函数组合的方式实现 ``` let _average = function(xs){ return fp.reduce(fp.add,0,xs) / xs.length } // 无需改动 let averageDollarValue = function(cars){ let dollar_value = fp.map(function(car){ return car.dollar_value }, cars) return _average(dollar_value) } ``` #### 4:使用flowRight写一个sanitizeNames()函数,返回一个下划线连接的小写字符串,把数组的name转化为这种形式:sanitizeNames(["Hello World"]) => ["hello_world"] ``` let _undersorce = fp.replace(/\W+/g,'_') //无需改动,并在sanitizeNames中使用它 ``` ### 代码三、基于以下代码完成下面四个练习 **答案见 code/test/index3.js** ``` // support.js class Container { static of (value) { return new Contiainer(value) } constructor(value) { this._value = value } map(fn) { return Container.of(fn(this._value)) } } class Maybe { static of (x) { return new Maybe(x) } isNothing() { return this._value === null || this._value === undefinded } constructor(x) { this._value = x } map(fn) { return this.isNothing? this: Maybe.of(fn(this._value)) } } module.exports = {Maybe, Container} ``` #### 1.使用fp.add(x,y)和fp.map(f,x)创建一个能在functor里的值增加的函数ex1 ``` // app.js const fp = require('lodash/fp') const {Maybe, Container} = require('./support) let maybe = Maybe.of([5, 6, 1]) let ex1 = ()=>{ // 你需要实现的函数 } ``` #### 2.实现一个函数ex2,能够使用fp.first获取列表第一个元素 ``` // app.js const fp = require('lodash/fp') const {Maybe, Container} = require('./support) let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do']) let ex2 = ()=>{ // 你需要实现的函数 } ``` #### 3.实现一个函数ex3,使用safeProp和fp.first找到user的名字首字母 ``` // app.js const fp = require('lodash/fp') const {Maybe, Container} = require('./support) let safeProp = fp.curry(function(x,o){ return Maybe.of(o[x]) }) let user = {id:2, name:'Albert'} let ex3 = ()=>{ // 你需要实现的函数 } ``` #### 4.使用Maybe重写ex4,不要有if语句 ``` // app.js const fp = require('lodash/fp') const {Maybe, Container} = require('./support) let ex4 = function(n){ if(n){ return parseInt(n) } } ``` ### 代码四、手写实现MyPromise源码 **答案见 code/test/index4.js 和 code/test/myPromise.js**