This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPromise{ | |
constructor(executor) { | |
this.status = "pending"; | |
this.value = undefined; | |
// This array is used to store all the onResolve functions in the chain call | |
this.resolveArr = []; | |
// // This array is used to store all the onReject functions in the chain call | |
this.rejectArr = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPromise{ | |
constructor(executor) { | |
this.status = "pending"; | |
this.value = undefined; | |
// This array is used to store all the onResolve functions in the chain call | |
this.resolveArr = []; | |
// // This array is used to store all the onReject functions in the chain call | |
this.rejectArr = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPromise{ | |
constructor(executor) { | |
// The initial state of the Promise object should be pending | |
this.status = "pending"; | |
// Store the value after success or reason after fail | |
this.value = undefined; | |
// Define a resolve function | |
let resolve = result => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function asyncToGenerator(generatorFunc) { | |
// A new function is returned | |
return function() { | |
// First call the generator function to generate the iterator | |
// Corresponding to: var gen = testG() | |
const gen = generatorFunc.apply(this, arguments) | |
// return a promise object | |
return new Promise((resolve, reject) => { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function asyncToGenerator(generatorFunc) { | |
return function() { | |
const gen = generatorFunc.apply(this, arguments) | |
return new Promise((resolve, reject) => { | |
function step(key, arg) { | |
let generatorResult | |
try { | |
generatorResult = gen[key](arg) | |
} catch (error) { | |
return reject(error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* testG() { | |
// await is translated as yield | |
const data = yield getData() | |
console.log('data: ', data); | |
const data2 = yield getData() | |
console.log('data2: ', data2); | |
return 'success' | |
} | |
var gen = testG() | |
var dataPromise = gen.next() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkAuth(data) { | |
if (data.role !== 'registered') { | |
console.log('The user is not a registered user'); | |
return false; | |
} | |
if (data.grade < 1) { | |
console.log("The user's level is less than 1"); | |
return false; | |
} | |
if (data.job !== 'FE') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jobList = ['FE', 'BE']; | |
var strategies = { | |
checkRole: function(value) { | |
if (value === 'registered') { | |
return true; | |
} | |
return false; | |
}, | |
checkGrade: function(value) { | |
if (value >= 1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var compose2 = function() { | |
var validator = new Validator(); | |
const data2 = { | |
role: 'register', | |
job: 'FE' | |
}; | |
validator.add(data2.role, 'checkRole'); | |
validator.add(data2.job, 'checkJob'); | |
const result = validator.check(); | |
return result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import yellowHOC from './yellowHOC'; | |
class TargetComponent extends Reac.Compoment { | |
render() { | |
return <div>hello world</div>; | |
} | |
} | |
export default yellowHOC(TargetComponent); |