Best JavaScript code snippet using wpt
promises-revision.js
Source:promises-revision.js
1// to perform asynchronous operation2// let p1 = Promise.resolve("p1 data");3// let p2 = Promise.resolve("p2");4// console.log(p1, p2);5// console.log("starting");6// setTimeout(() => {7// console.log("set time out");8// }, 0);9// Promise.resolve(1)10// .then(() => console.log("promise1 resol"))11// .catch((err) => console.log("catching 1 promise"));12// Promise.resolve(1).then(() => console.log("promise2"));13// Promise.resolve(1).then(() => console.log("promise3"));14// Promise.resolve(1).then(() => console.log("promise4"));15// Promise.resolve(1).then(() => console.log("promise1"));16// Promise.resolve(1).then(() => console.log("promise2"));17// Promise.resolve(1).then(() => console.log("promise3"));18// Promise.resolve(1).then(() => console.log("promise4"));19// Promise.resolve(1).then(() => console.log("promise1"));20// Promise.resolve(1).then(() => console.log("promise2"));21// Promise.resolve(1).then(() => console.log("promise3"));22// Promise.resolve(1).then(() => console.log("promise4"));23// Promise.resolve(1).then(() => console.log("promise1"));24// Promise.resolve(1).then(() => console.log("promise2"));25// Promise.resolve(1).then(() => console.log("promise3"));26// Promise.resolve(1).then(() => console.log("promise4"));27// Promise.resolve(1).then(() => console.log("promise1"));28// Promise.resolve(1).then(() => console.log("promise2"));29// Promise.resolve(1).then(() => console.log("promise3"));30// Promise.resolve(1).then(() => console.log("promise4"));31// Promise.resolve(1).then(() => console.log("promise1"));32// Promise.resolve(1).then(() => console.log("promise2"));33// Promise.resolve(1).then(() => console.log("promise3"));34// Promise.resolve(1).then(() => console.log("promise4"));35// Promise.resolve(1).then(() => console.log("promise1"));36// Promise.resolve(1).then(() => console.log("promise2"));37// Promise.resolve(1).then(() => console.log("promise3"));38// Promise.resolve(1).then(() => console.log("promise4"));39// Promise.resolve(1).then(() => console.log("promise1"));40// Promise.resolve(1).then(() => console.log("promise2"));41// Promise.resolve(1).then(() => console.log("promise3"));42// Promise.resolve(1).then(() => console.log("promise4"));43// Promise.resolve(1).then(() => console.log("promise1"));44// Promise.resolve(1).then(() => console.log("promise2"));45// Promise.resolve(1).then(() => console.log("promise3"));46// Promise.resolve(1).then(() => console.log("promise4"));47// Promise.resolve(1).then(() => console.log("promise1"));48// Promise.resolve(1).then(() => console.log("promise2"));49// Promise.resolve(1).then(() => console.log("promise3"));50// Promise.resolve(1).then(() => console.log("promise4"));51// Promise.resolve(1).then(() => console.log("promise1"));52// Promise.resolve(1).then(() => console.log("promise2"));53// Promise.resolve(1).then(() => console.log("promise3"));54// Promise.resolve(1).then(() => console.log("promise4"));55// Promise.resolve(1).then(() => console.log("promise1"));56// Promise.resolve(1).then(() => console.log("promise2"));57// Promise.resolve(1).then(() => console.log("promise3"));58// Promise.resolve(1).then(() => console.log("promise4"));59// Promise.resolve(1).then(() => console.log("promise1"));60// Promise.resolve(1).then(() => console.log("promise2"));61// Promise.resolve(1).then(() => console.log("promise3"));62// Promise.resolve(1).then(() => console.log("promise4"));63// Promise.resolve(1).then(() => console.log("promise1"));64// Promise.resolve(1).then(() => console.log("promise2"));65// Promise.resolve(1).then(() => console.log("promise3"));66// Promise.resolve(1).then(() => console.log("promise4"));67// Promise.resolve(1).then(() => console.log("promise1"));68// Promise.resolve(1).then(() => console.log("promise2"));69// Promise.resolve(1).then(() => console.log("promise3"));70// Promise.resolve(1).then(() => console.log("promise4"));71// Promise.resolve(1).then(() => console.log("promise1"));72// Promise.resolve(1).then(() => console.log("promise2"));73// Promise.resolve(1).then(() => console.log("promise3"));74// Promise.resolve(1).then(() => console.log("promise4"));75// Promise.resolve(1).then(() => console.log("promise1"));76// Promise.resolve(1).then(() => console.log("promise2"));77// Promise.resolve(1).then(() => console.log("promise3"));78// Promise.resolve(1).then(() => console.log("promise4"));79// Promise.resolve(1).then(() => console.log("promise1"));80// Promise.resolve(1).then(() => console.log("promise2"));81// Promise.resolve(1).then(() => console.log("promise3"));82// Promise.resolve(1).then(() => console.log("promise4"));83// Promise.resolve(1).then(() => console.log("promise1"));84// Promise.resolve(1).then(() => console.log("promise2"));85// Promise.resolve(1).then(() => console.log("promise3"));86// Promise.resolve(1).then(() => console.log("promise4"));87// Promise.resolve(1).then(() => console.log("promise1"));88// Promise.resolve(1).then(() => console.log("promise2"));89// Promise.resolve(1).then(() => console.log("promise3"));90// Promise.resolve(1).then(() => console.log("promise4"));91// Promise.resolve(1).then(() => console.log("promise1"));92// Promise.resolve(1).then(() => console.log("promise2"));93// Promise.resolve(1).then(() => console.log("promise3"));94// Promise.resolve(1).then(() => console.log("promise4"));95// Promise.resolve(1).then(() => console.log("promise1"));96// Promise.resolve(1).then(() => console.log("promise2"));97// Promise.resolve(1).then(() => console.log("promise3"));98// Promise.resolve(1).then(() => console.log("promise4"));99// Promise.resolve(1).then(() => console.log("promise1"));100// Promise.resolve(1).then(() => console.log("promise2"));101// Promise.resolve(1).then(() => console.log("promise3"));102// Promise.resolve(1).then(() => console.log("promise4"));103// Promise.resolve(1).then(() => console.log("promise1"));104// Promise.resolve(1).then(() => console.log("promise2"));105// Promise.resolve(1).then(() => console.log("promise3"));106// Promise.resolve(1).then(() => console.log("promise4"));107// Promise.resolve(1).then(() => console.log("promise1"));108// Promise.resolve(1).then(() => console.log("promise2"));109// Promise.resolve(1).then(() => console.log("promise3"));110// Promise.resolve(1).then(() => console.log("promise4"));111// Promise.resolve(1).then(() => console.log("promise1"));112// Promise.resolve(1).then(() => console.log("promise2"));113// Promise.resolve(1).then(() => console.log("promise3"));114// Promise.resolve(1).then(() => console.log("promise4"));115// Promise.resolve(1).then(() => console.log("promise1"));116// Promise.resolve(1).then(() => console.log("promise2"));117// Promise.resolve(1).then(() => console.log("promise3"));118// Promise.resolve(1).then(() => console.log("promise4"));119// Promise.resolve(1).then(() => console.log("promise1"));120// Promise.resolve(1).then(() => console.log("promise2"));121// Promise.resolve(1).then(() => console.log("promise3"));122// Promise.resolve(1).then(() => console.log("promise4"));123// Promise.resolve(1).then(() => console.log("promise1"));124// Promise.resolve(1).then(() => console.log("promise2"));125// Promise.resolve(1).then(() => console.log("promise3"));126// Promise.resolve(1).then(() => console.log("promise4"));127// Promise.resolve(1).then(() => console.log("promise1"));128// Promise.resolve(1).then(() => console.log("promise2"));129// Promise.resolve(1).then(() => console.log("promise3"));130// Promise.resolve(1).then(() => console.log("promise4"));131// Promise.resolve(1).then(() => console.log("promise1"));132// Promise.resolve(1).then(() => console.log("promise2"));133// Promise.resolve(1).then(() => console.log("promise3"));134// Promise.resolve(1).then(() => console.log("promise4"));135// Promise.resolve(1).then(() => console.log("promise1"));136// Promise.resolve(1).then(() => console.log("promise2"));137// Promise.resolve(1).then(() => console.log("promise3"));138// Promise.resolve(1).then(() => console.log("promise4"));139// Promise.resolve(1).then(() => console.log("promise1"));140// Promise.resolve(1).then(() => console.log("promise2"));141// Promise.resolve(1).then(() => console.log("promise3"));142// Promise.resolve(1).then(() => console.log("promise4"));143// Promise.resolve(1).then(() => console.log("promise1"));144// Promise.resolve(1).then(() => console.log("promise2"));145// Promise.resolve(1).then(() => console.log("promise3"));146// Promise.resolve(1).then(() => console.log("promise4"));147// Promise.resolve(1).then(() => console.log("promise1"));148// Promise.resolve(1).then(() => console.log("promise2"));149// Promise.resolve(1).then(() => console.log("promise3"));150// Promise.resolve(1).then(() => console.log("promise4"));151// Promise.resolve(1).then(() => console.log("promise1"));152// Promise.resolve(1).then(() => console.log("promise2"));153// Promise.resolve(1).then(() => console.log("promise3"));154// Promise.resolve(1).then(() => console.log("promise4"));155// Promise.resolve(1).then(() => console.log("promise1"));156// Promise.resolve(1).then(() => console.log("promise2"));157// Promise.resolve(1).then(() => console.log("promise3"));158// Promise.resolve(1).then(() => console.log("promise4"));159// Promise.resolve(1).then(() => console.log("promise1"));160// Promise.resolve(1).then(() => console.log("promise2"));161// Promise.resolve(1).then(() => console.log("promise3"));162// Promise.resolve(1).then(() => console.log("promise4"));163// Promise.resolve(1).then(() => console.log("promise1"));164// Promise.resolve(1).then(() => console.log("promise2"));165// Promise.resolve(1).then(() => console.log("promise3"));166// Promise.resolve(1).then(() => console.log("promise4"));167// Promise.resolve(1).then(() => console.log("promise1"));168// Promise.resolve(1).then(() => console.log("promise2"));169// Promise.resolve(1).then(() => console.log("promise3"));170// Promise.resolve(1).then(() => console.log("promise4"));171// Promise.resolve(1).then(() => console.log("promise1"));172// Promise.resolve(1).then(() => console.log("promise2"));173// Promise.resolve(1).then(() => console.log("promise3"));174// Promise.resolve(1).then(() => console.log("promise4"));175// Promise.resolve(1).then(() => console.log("promise1"));176// Promise.resolve(1).then(() => console.log("promise2"));177// Promise.resolve(1).then(() => console.log("promise3"));178// Promise.resolve(1).then(() => console.log("promise4"));179// Promise.resolve(1).then(() => console.log("promise1"));180// Promise.resolve(1).then(() => console.log("promise2"));181// Promise.resolve(1).then(() => console.log("promise3"));182// Promise.resolve(1).then(() => console.log("promise4"));183// Promise.resolve(1).then(() => console.log("promise1"));184// Promise.resolve(1).then(() => console.log("promise2"));185// Promise.resolve(1).then(() => console.log("promise3"));186// Promise.resolve(1).then(() => console.log("promise4"));187// Promise.resolve(1).then(() => console.log("promise1"));188// Promise.resolve(1).then(() => console.log("promise2"));189// Promise.resolve(1).then(() => console.log("promise3"));190// Promise.resolve(1).then(() => console.log("promise4"));191// Promise.resolve(1).then(() => console.log("promise1"));192// Promise.resolve(1).then(() => console.log("promise2"));193// Promise.resolve(1).then(() => console.log("promise3"));194// Promise.resolve(1).then(() => console.log("promise4"));195// Promise.resolve(1).then(() => console.log("promise1"));196// Promise.resolve(1).then(() => console.log("promise2"));197// Promise.resolve(1).then(() => console.log("promise3"));198// Promise.resolve(1).then(() => console.log("promise4"));199// Promise.resolve(1).then(() => console.log("promise1"));200// Promise.resolve(1).then(() => console.log("promise2"));201// Promise.resolve(1).then(() => console.log("promise3"));202// Promise.resolve(1).then(() => console.log("promise4"));203// Promise.resolve(1).then(() => console.log("promise1"));204// Promise.resolve(1).then(() => console.log("promise2"));205// Promise.resolve(1).then(() => console.log("promise3"));206// Promise.resolve(1).then(() => console.log("promise4"));207// Promise.resolve(1).then(() => console.log("promise1"));208// Promise.resolve(1).then(() => console.log("promise2"));209// Promise.resolve(1).then(() => console.log("promise3"));210// Promise.resolve(1).then(() => console.log("promise4"));211// Promise.resolve(1).then(() => console.log("promise1"));212// Promise.resolve(1).then(() => console.log("promise2"));213// Promise.resolve(1).then(() => console.log("promise3"));214// Promise.resolve(1).then(() => console.log("promise4"));215// Promise.resolve(1).then(() => console.log("promise1"));216// Promise.resolve(1).then(() => console.log("promise2"));217// Promise.resolve(1).then(() => console.log("promise3"));218// Promise.resolve(1).then(() => console.log("promise4"));219// Promise.resolve(1).then(() => console.log("promise1"));220// Promise.resolve(1).then(() => console.log("promise2"));221// Promise.resolve(1).then(() => console.log("promise3"));222// Promise.resolve(1).then(() => console.log("promise4"));223// Promise.resolve(1).then(() => console.log("promise1"));224// Promise.resolve(1).then(() => console.log("promise2"));225// Promise.resolve(1).then(() => console.log("promise3"));226// Promise.resolve(1).then(() => console.log("promise4"));227// Promise.resolve(1).then(() => console.log("promise1"));228// Promise.resolve(1).then(() => console.log("promise2"));229// Promise.resolve(1).then(() => console.log("promise3"));230// Promise.resolve(1).then(() => console.log("promise4"));231// Promise.resolve(1).then(() => console.log("promise1"));232// Promise.resolve(1).then(() => console.log("promise2"));233// Promise.resolve(1).then(() => console.log("promise3"));234// Promise.resolve(1).then(() => console.log("promise4"));235// Promise.resolve(1).then(() => console.log("promise1"));236// Promise.resolve(1).then(() => console.log("promise2"));237// Promise.resolve(1).then(() => console.log("promise3"));238// Promise.resolve(1).then(() => console.log("promise4"));239// Promise.resolve(1).then(() => console.log("promise1"));240// Promise.resolve(1).then(() => console.log("promise2"));241// Promise.resolve(1).then(() => console.log("promise3"));242// Promise.resolve(1).then(() => console.log("promise4"));243// Promise.resolve(1).then(() => console.log("promise1"));244// Promise.resolve(1).then(() => console.log("promise2"));245// Promise.resolve(1).then(() => console.log("promise3"));246// Promise.resolve(1).then(() => console.log("promise4"));247// Promise.resolve(1).then(() => console.log("promise1"));248// Promise.resolve(1).then(() => console.log("promise2"));249// Promise.resolve(1).then(() => console.log("promise3"));250// Promise.resolve(1).then(() => console.log("promise4"));251// Promise.resolve(1).then(() => console.log("promise1"));252// Promise.resolve(1).then(() => console.log("promise2"));253// Promise.resolve(1).then(() => console.log("promise3"));254// Promise.resolve(1).then(() => console.log("promise4"));255// Promise.resolve(1).then(() => console.log("promise1"));256// Promise.resolve(1).then(() => console.log("promise2"));257// Promise.resolve(1).then(() => console.log("promise3"));258// Promise.resolve(1).then(() => console.log("promise4"));259// Promise.resolve(1).then(() => console.log("promise1"));260// Promise.resolve(1).then(() => console.log("promise2"));261// Promise.resolve(1).then(() => console.log("promise3"));262// Promise.resolve(1).then(() => console.log("promise4"));263// Promise.resolve(1).then(() => console.log("promise1"));264// Promise.resolve(1).then(() => console.log("promise2"));265// Promise.resolve(1).then(() => console.log("promise3"));266// Promise.resolve(1).then(() => console.log("promise4"));267// Promise.resolve(1).then(() => console.log("promise1"));268// Promise.resolve(1).then(() => console.log("promise2"));269// Promise.resolve(1).then(() => console.log("promise3"));270// Promise.resolve(1).then(() => console.log("promise4"));271// Promise.resolve(1).then(() => console.log("promise1"));272// Promise.resolve(1).then(() => console.log("promise2"));273// Promise.resolve(1).then(() => console.log("promise3"));274// Promise.resolve(1).then(() => console.log("promise4"));275// Promise.resolve(1).then(() => console.log("promise1"));276// Promise.resolve(1).then(() => console.log("promise2"));277// Promise.resolve(1).then(() => console.log("promise3"));278// Promise.resolve(1).then(() => console.log("promise4"));279// Promise.resolve(1).then(() => console.log("promise1"));280// Promise.resolve(1).then(() => console.log("promise2"));281// Promise.resolve(1).then(() => console.log("promise3"));282// Promise.resolve(1).then(() => console.log("promise4"));283// Promise.resolve(1).then(() => console.log("promise1"));284// Promise.resolve(1).then(() => console.log("promise2"));285// Promise.resolve(1).then(() => console.log("promise3"));286// Promise.resolve(1).then(() => console.log("promise4"));287// Promise.resolve(1).then(() => console.log("promise1"));288// Promise.resolve(1).then(() => console.log("promise2"));289// Promise.resolve(1).then(() => console.log("promise3"));290// Promise.resolve(1).then(() => console.log("promise4"));291// Promise.resolve(1).then(() => console.log("promise1"));292// Promise.resolve(1).then(() => console.log("promise2"));293// Promise.resolve(1).then(() => console.log("promise3"));294// Promise.resolve(1).then(() => console.log("promise4"));295// Promise.resolve(1).then(() => console.log("promise1"));296// Promise.resolve(1).then(() => console.log("promise2"));297// Promise.resolve(1).then(() => console.log("promise3"));298// Promise.resolve(1).then(() => console.log("promise4"));299// Promise.resolve(1).then(() => console.log("promise1"));300// Promise.resolve(1).then(() => console.log("promise2"));301// Promise.resolve(1).then(() => console.log("promise3"));302// Promise.resolve(1).then(() => console.log("promise4"));303// Promise.resolve(1).then(() => console.log("promise1"));304// Promise.resolve(1).then(() => console.log("promise2"));305// Promise.resolve(1).then(() => console.log("promise3"));306// Promise.resolve(1).then(() => console.log("promise4"));307// Promise.resolve(1).then(() => console.log("promise1"));308// Promise.resolve(1).then(() => console.log("promise2"));309// Promise.resolve(1).then(() => console.log("promise3"));310// Promise.resolve(1).then(() => console.log("promise4"));311// Promise.resolve(1).then(() => console.log("promise1"));312// Promise.resolve(1).then(() => console.log("promise2"));313// Promise.resolve(1).then(() => console.log("promise3"));314// Promise.resolve(1).then(() => console.log("promise4"));315// Promise.resolve(1).then(() => console.log("promise1"));316// Promise.resolve(1).then(() => console.log("promise2"));317// Promise.resolve(1).then(() => console.log("promise3"));318// Promise.resolve(1).then(() => console.log("promise4"));319// Promise.resolve(1).then(() => console.log("promise1"));320// Promise.resolve(1).then(() => console.log("promise2"));321// Promise.reject(1)322// .then(() => console.log("promise3"))323// .catch((err) => console.log("catching"));324// Promise.resolve(1).then(() =>325// console.log("promise4fbsjafbkajsbfkasabfkakjfkjabbfka")326// );327// Promise.resolve(1).then(() => console.log("promise1 ----vasanth"));328// Promise.resolve(1).then(() => console.log("promise2"));329// Promise.resolve(1).then(() => console.log("promise3"));330// Promise.resolve(1).then(() => console.log("promise4"));331// Promise.resolve(1).then(() => console.log("promise1"));332// Promise.resolve(1).then(() => console.log("promise2"));333// Promise.resolve(1).then(() => console.log("promise3"));334// Promise.resolve(1).then(() => console.log("promise4"));335// Promise.resolve(1).then(() => console.log("promise1"));336// Promise.resolve(1).then(() => console.log("promise2"));337// Promise.resolve(1).then(() => console.log("promise3"));338// Promise.resolve(1).then(() => console.log("promise4"));339// Promise.resolve(1).then(() => console.log("promise1"));340// Promise.resolve(1).then(() => console.log("promise2"));341// Promise.resolve(1).then(() => console.log("promise3"));342// Promise.resolve(1).then(() => console.log("promise4"));343// console.log("end");344// classes -> Multi paradigm programming language345// OOPs -> Object Oriented Programming system346// functional programming347// class == function348// class is a syntactic sugar of function349// async await is a syntactic of promise350// function Person(name, age) {351// this.name = name;352// this.age = age;353// this.getAge = function () {354// return this.age;355// };356// console.log(this);357// // return this; default return this358// }359// const vasanth = new Person("vasanth", 20);360// console.log("this return", vasanth);361// const Hema = new Person("Hema", 6);362// const Amit = new Person("Amit", 2);363//364// class Person {365// constructor(name, age) {366// this.name = name;367// this.age = age;368// }369// // getter370// get fullName() {371// return this.name + " kumar";372// }373// set setFullName(name) {374// this.name = name;375// }376// getAge() {377// return this.age;378// }379// }380// let vasanth = new Person("vasanth", 20);381// console.log(vasanth.age, vasanth.fullName);382// vasanth.setFullName = "Rajesh";383// console.log(vasanth.fullName);384// Person();385// class Person1 {386// constructor() {387// console.log("person in class");388// }389// }390// new Person1();391// function Person() {392// console.log("person in fn ");393// }394// function declarations are hoisted but classes are not hoisted395// class -> it is a blue print396// objects -> instance of class or real time entity397// class Human {398// constructor(name, age, gender) {399// this.name = name;400// this.age = age;401// this.gender = gender;402// }403// eat() {404// console.log("eating ");405// }406// sleep() {407// console.log("sleeping");408// }409// think() {410// // console.log("thinking");411// return "thinking";412// }413// }414// let vasanth = new Human("vasanth", 10, "Male");415// console.log(vasanth, vasanth.think());416// let Rani = new Human("rani", 14, "Female");417// class Car {418// constructor(model, color, brand, type) {419// this.model = model;420// this.color = color;421// this.brand = brand;422// this.type = type;423// }424// run() {425// console.log("running");426// }427// playSongs() {428// console.log("songs");429// }430// }431// let hondaCity = new Car("2022", "red", "honda", "sedan");432// let rangeaRover = new Car("2022", "red yellow", "tata", "SUV");433// inheritance434// class GrandParent {435// house() {436// console.log("house");437// }438// }439// class Parent extends GrandParent {440// car() {441// console.log("maruti espresso");442// }443// }444// class Child extends Parent {445// bike() {446// console.log("yamaha");447// }448// // polymorphism -> method overriding449// car() {450// console.log("audi");451// }452// }453// let vinayagam = new GrandParent();454// vinayagam.house();455// let vasanth = new Parent();456// let aadhitya = new Child();457// vasanth.car();458// aadhitya.car();459// // // aadhitya.bike();460// vasanth.house();461// aadhitya.house();`462// function Person(name) {463// this.name = name;464// this.getName = function () {465// return this.name;466// };467// this.num = 10;468// }469class Person {470 constructor(name) {471 this.name = name;472 }473 num = 10;474 getName() {475 return this.name;476 }477}478// let vasanth = new Person("vasanth");479// let anish = new Person("anish");480// console.log(vasanth.getName(), vasanth.num);481// class Laptop {482// constructor(ram, rom, brand, size = 15.4) {483// this.ram = ram;484// this.rom = rom;485// this.brand = brand;486// this.size = size;487// }488// }489// let macbookAir = new Laptop("4gb", 128, "apple", 13.3);490// let acer123 = new Laptop("4gb", 128, "acer");491// let dell = new Laptop("4gb", 128, "dell");492// console.log(macbookAir, acer123, dell);493// prototypical inheritance494// polyfill495// method496// fetch(), axios()497// jquery -> react, angular498// css advanced499// call, bind, apply500// currying501// ()()()502function getFact(num) {503 if (num == 1) {504 return 1;505 }506 return num * getFact(num - 1);507}508console.time();509console.log(getFact(50000000000));...
index3.js
Source:index3.js
1// 解å³é¾å¼è°ç¨çé®é¢ï¼å¤å å®ç°catch2const MyPromise3 = require('../MyPromise/promise3.js')3// å¤çå¼æ¥åå¤æ¬¡è°ç¨çé®é¢4let promise = new MyPromise3((resolve, reject) => {5 resolve('resolve')6 // reject('fail')7})8// éè¿ return ä¼ éç»æ9promise.then(value => {10 console.log('first' + value) // first resolve11 return 'second' + value // æ®éå¼12}).then(value => {13 console.log(value) // second resolve14})15// éè¿æ°ç promise resolve ç»æ16promise.then(value => {17 console.log('first' + value) // first resolve18 return value // æ®éå¼19}).then(value => {20 console.log('second' + value) // second resolve21 return new MyPromise3((resolve, reject) => {22 setTimeout(() => {23 resolve(value)24 }, 2000)25 })26}).then(value => {27 console.log('third' + value) // third resolve28})29// éè¿æ°ç promise reject30promise.then(value => {31 console.log('first' + value) // first resolve32 return value // æ®éå¼33}).then(value => {34 console.log('second' + value) // second resolve35 return new MyPromise3((resolve, reject) => {36 setTimeout(() => {37 reject('ERROR')38 }, 2000)39 })40}).then(value => {41 console.log('third' + value) // ä¸æ§è¡42}, reason => {43 console.log(reason) // ERROR44})45// then èµ°äºå¤±è´¥çåè°å½æ°ä¹åï¼åèµ°then46promise.then(value => {47 console.log('first' + value) // first resolve48 return value // æ®éå¼49}).then(value => {50 console.log('second' + value) // second resolve51 return new MyPromise3((resolve, reject) => {52 setTimeout(() => {53 reject('ERROR')54 }, 2000)55 })56}).then(value => {57 console.log('third' + value) // ä¸æ§è¡58}, reason => {59 console.log(reason) // ERROR60 // é»è®¤return undefined61}).then(value => {62 console.log(value) // undefinedï¼å 为ä¸ä¸ä¸ªthen没æè¿åå¼63}, reason => {64 console.log(reason) // ä¸æ§è¡65})66// then ä¸ä½¿ç¨ throw Error67promise.then(value => {68 console.log('first' + value) // first resolve69 return value // æ®éå¼70}).then(value => {71 console.log('second' + value) // second resolve72 return new MyPromise3((resolve, reject) => {73 setTimeout(() => {74 reject('ERROR')75 }, 2000)76 })77}).then(value => {78 console.log('third' + value) // ä¸æ§è¡79}, reason => {80 console.log(reason) // ERROR81 // é»è®¤return undefined82}).then(value => {83 console.log(value) // undefined84 throw new Error('æå¨æåºé误')85}, reason => {86 console.log(reason) // ä¸æ§è¡87}).then(value => {88 console.log(value) // ä¸æ§è¡89}, reason => {90 console.log(reason) // æå¨æåºé误91})92// Promiseåµå¥93promise.then(() => {94 return new MyPromise3((resolve, reject) => {95 resolve(new MyPromise3((resolve, reject) => {96 resolve(new MyPromise3((resolve, reject) => {97 resolve(111)98 }))99 }))100 })101}).then(value => {102 console.log(value) // 111103})104promise.then(() => {105 return new MyPromise3((resolve, reject) => {106 resolve(new MyPromise3((resolve, reject) => {107 reject(new MyPromise3((resolve, reject) => {108 resolve(111)109 }))110 }))111 })112}).then(value => {113 console.log(value) // ä¸æ§è¡114}, reason => {115 console.log('fail', reason) // æå°ä¸ä¸ªpromise对象ï¼å 为æºç å±é¢rejectçç»æä¸ä¼è¿è¡éå½å¤çï¼æ以æ æ³æ¿å°promiseæåçå¼116})117// 空çthen118promise.then().then().then(value => {119 console.log('success: ', value) // success: resolve120}, reason => {121 console.log('fail: ', reason)122})123// ç¨catch æè·å¼å¸¸124promise.then(value => {125 console.log('first' + value) // first resolve126 return value // æ®éå¼127}).then(value => {128 console.log('second' + value) // second resolve129 return new MyPromise3((resolve, reject) => {130 setTimeout(() => {131 reject('ERROR')132 }, 2000)133 })134}).then(value => {135 console.log('third' + value) // ä¸æ§è¡136}, reason => {137 console.log(reason) // ERROR138 // é»è®¤return undefined139}).then(value => {140 console.log(value) // undefined141 throw new Error('æå¨æåºé误')142}, reason => {143 console.log(reason) // ä¸æ§è¡144}).then(value => {145 console.log(value) // ä¸æ§è¡146}147// , reason => {148// console.log('then:', reason) // then æå¨æåºé误149// }150)151.catch(reason => {152 console.log('catch:', reason) // catch: æå¨æåºé误ï¼å¦æä¸ä¸ªthenä¼ äºå¤±è´¥åè°ï¼è¿éå°±ä¸èµ°153 return 'catch error'154}).then(value => {155 console.log('then: ', value) // then: catch error 156})157// catch å¨ Promise çæºç å±é¢ä¸å°±æ¯ä¸ä¸ªthenï¼ catchä¹æ¯éµå¾ª thençè¿è¡åå158// æåçæ¡ä»¶159// then return æ®éç javascript value160// then return æ°çpromiseæåæçç»æ value161// 失败çæ¡ä»¶162// then return æ°çpromise失败æçåå reason163// then æåºäºå¼å¸¸ throw new Error164// promise é¾å¼è°ç¨165// javascript return this166// then ä¸å
·å¤this ï¼éè¦è¿åä¸ä¸ªæ°ç Promiseï¼ä»¥è¾¾å°é¾å¼è°ç¨167// å°è¿åå¼ä¼ªé 为promise对象168const promise111 = new MyPromise3((resolve, reject) => {169 resolve(111)170})171promise111.then(res => {172 console.log(res) // 111173 return {174 // æ
æé»æºç ç空åï¼è®©å®è¯¯å¤æè¿åæ¯ä¸ä¸ªpromise对象ï¼è¿ä¼å¯¼è´åé¢çthenä¸æ§è¡ï¼å 为ä¸ç´æ¯ PENDING ç¶æ175 // æºç ä¸æ§è¡å° then.call(...) å°±ç»æäºï¼èµ°ä¸å° then çåè°åæ°éé¢ï¼å 为è¿ä¸ªåç then 没æåè°åæ°ï¼å¦ææ¯çç thenï¼å³ä½¿æ²¡æä¼ åè°åæ°ï¼æºç éä¹ä¼èªå¨çæåè°åæ°ï¼å°±ç®åç then æåè°åæ°ï¼ä¹æ²¡ææ§è¡åè°åæ°çé»è¾ï¼æä»¥ä¼ äºä¹ç¸å½äºæ²¡ä¼ ï¼ï¼æ以ä¹æ æ³æ´æ¹ç¶æï¼ä¸ç´æ¯ PENDINGï¼æ以åé¢ççthençåè°åæ°æ æ³æ§è¡176 then: function () {177 console.log(res, 222) // 111, 222178 }179 }180}).then(resolve => {181 console.log(resolve, 333) // æ æ³æ§è¡182}, err => {183 console.log(err, 666) // æ æ³æ§è¡184})185// æµè¯ä¸å¼å§å°± resolve ä¸ä¸ª Promise 对象186const p = new MyPromise3((resolve, reject) => {187 resolve(new MyPromise3((resolve, reject) => {188 resolve(new MyPromise3((resolve, reject) => {189 setTimeout(() => {190 resolve('success')191 }, 1000)192 }))193 }))194})195p.then((res) => {196 console.log(res)...
手写一个Promise2.js
Source:手写一个Promise2.js
1function Promise(executor){2 let that = this;3 that.status = 'pending';4 that.value = null;5 that.reason = null;6 that.onFilFulledCallbacks = [];7 that.onRejectedCallbacks = [];8 function resolve(value){9 if(that.status === 'pending'){10 that.status = 'resolved';11 that.value = value;12 that.onFilFulledCallbacks.forEach((fn)=>{13 fn();14 });15 }16 }17 function reject(reason){18 if(that.status === 'pending'){19 that.status = 'rejected';20 that.reason = reason;21 that.onRejectedCallbacks.forEach((fn)=>{22 fn();23 });24 }25 }26 try{27 executor(resolve,reject);28 }catch(e){29 reject(e);30 }31}32Promise.prototype.then = function(onFilfulled,onRejected){33 onFilfulled = typeof onFilfulled === 'function'?onFilfulled:value=>value;34 onRejected = typeof onRejected === 'function'?onRejected:err=>{throw err};35 let that = this;36 let promise2 = new Promise((resolve,reject)=>{37 if(that.status === 'resolved'){38 setTimeout(()=>{39 try{40 let promise3 = onFilfulled(that.value);41 resolvePromiseRelation(promise2,promise3,resolve,reject);42 }catch(e){43 reject(e);44 }45 },0);46 }47 if(that.status === 'rejected'){48 setTimeout(()=>{49 try{50 let promise3 = onRejected(that.reason);51 resolvePromiseRelation(promise2,promise3,resolve,reject);52 }catch(e){53 reject(e);54 }55 },0);56 }57 if(that.status === 'pending'){58 that.onFilFulledCallbacks.push(function(){59 setTimeout(()=>{60 try{61 let promise3 = onFilfulled(that.value);62 resolvePromiseRelation(promise2,promise3,resolve,reject);63 }catch(e){64 reject(e);65 }66 },0);67 });68 that.onRejectedCallbacks.push(function(){69 setTimeout(()=>{70 try{71 let promise3 = onRejected(that.reason);72 resolvePromiseRelation(promise2,promise3,resolve,reject);73 }catch(e){74 reject(e);75 }76 },0);77 });78 }79 });80 return promise2;81}82function resolvePromiseRelation(promise2,promise3,resolve,reject){83 if(promise2 == promise3){84 return reject(new TypeError('循ç¯å¼ç¨äº!'));85 }86 let called;87 if(promise3!==null&&(typeof promise3 === 'object' || typeof promise3 === 'function')){88 try{89 let then = promise3.then;90 if(typeof then === 'function'){91 then.call(promise3, (promise4)=>{92 if(called) return;93 called = true;94 resolvePromiseRelation(promise2,promise4,resolve,reject);95 },(r)=>{96 if(called) return;97 called = true;98 reject(r);99 });100 }else{101 resolve(promise3);102 }103 }catch(e){104 if(called) return;105 called = true;106 reject(e);107 };108 }else{109 resolve(promise3);110 }111}112Promise.prototype.catch = function(errFn){113 return this.then(null,errFn);114}115Promise.prototype.finally = function(fn){116 this.then(()=>{117 fn();118 },()=>{119 fn();120 });121 return this;122}123Promise.all = function(values){124 return new Promise((resolve,reject)=>{125 let results = [];126 let index = 0;127 function addToArr(key,value){128 index++;129 results[key] = value;130 if(index === values.length){131 resolve(results);132 }133 }134 for(let i = 0; i < values.length; i++){135 let current = values[i];136 if(current && current.then && typeof current.then === 'function'){137 current.then((value)=>{138 addToArr(i,value);139 },reject);140 }else{141 addToArr(i,current);142 }143 }144 });145}146Promise.race = function(values){147 return new Promise((resolve,reject)=>{148 for(let i = 0; i < values.length; i++){149 let current = values[i];150 if(current&¤t.then&&typeof current.then === 'function'){151 current.then(resolve,reject);152 }else{153 resolve(current);154 }155 }156 });157}158Promise.resolve = function(value){159 return new Promise((resolve,reject)=>{160 resolve(value);161 });162}163Promise.reject = function(reason){164 return new Promise((resolve,reject)=>{165 reject(reason);166 });167}168Promise.defer = Promise.deferred = function(){169 let dfd = {};170 dfd.promise = new Promise((resolve, reject)=>{171 dfd.resolve = resolve;172 dfd.reject = reject;173 });174 return dfd;175}176Promise.reject(new Error(111))177// Promise.reject(new Error(111))178 .then(res => { console.log(`then1: ${res}`); return 2; })179 // .catch(res => { console.log(`catch1: ${res}`); })180 .finally(function () { console.log("finally") });...
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3page.get(function(err, info) {4 if (err) {5 console.log(err);6 } else {7 console.log(info);8 }9});10{ [Error: ENOENT, no such file or directory 'test.js']11 path: 'test.js' }
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var csv = require('fast-csv');4var stream = fs.createReadStream("./data.csv");5var csvStream = csv()6 .on("data", function(data){7 var page = wptools.page(data[0]);8 page.getPromise().then(function(doc) {9 var json = doc.json();10 var infobox = json.infobox;11 var geo = json.geo;12 var categories = json.categories;13 var pageid = json.pageid;14 var title = json.title;15 var url = json.url;16 var wikibase = json.wikibase;17 var wikidata = json.wikidata;18 var extlinks = json.extlinks;19 var images = json.images;20 var langlinks = json.langlinks;21 var links = json.links;22 var pageprops = json.pageprops;23 var sections = json.sections;24 var text = json.text;25 var references = json.references;26 var pageid = json.pageid;27 var title = json.title;28 var url = json.url;29 var wikibase = json.wikibase;30 var wikidata = json.wikidata;31 var extlinks = json.extlinks;32 var images = json.images;33 var langlinks = json.langlinks;34 var links = json.links;35 var pageprops = json.pageprops;36 var sections = json.sections;37 var text = json.text;38 var references = json.references;39 var pageid = json.pageid;40 var title = json.title;41 var url = json.url;42 var wikibase = json.wikibase;43 var wikidata = json.wikidata;44 var extlinks = json.extlinks;45 var images = json.images;46 var langlinks = json.langlinks;47 var links = json.links;48 var pageprops = json.pageprops;49 var sections = json.sections;50 var text = json.text;51 var references = json.references;52 var pageid = json.pageid;53 var title = json.title;54 var url = json.url;55 var wikibase = json.wikibase;56 var wikidata = json.wikidata;57 var extlinks = json.extlinks;58 var images = json.images;59 var langlinks = json.langlinks;
Using AI Code Generation
1var wptools = require("wptools");2var fs = require('fs');3var data = require('./data.json');4var data2 = require('./data2.json');5var data3 = require('./data3.json');6var data4 = require('./data4.json');7var data5 = require('./data5.json');8var data6 = require('./data6.json');9var data7 = require('./data7.json');10var data8 = require('./data8.json');11var data9 = require('./data9.json');12var data10 = require('./data10.json');13var data11 = require('./data11.json');14var data12 = require('./data12.json');15var data13 = require('./data13.json');16var data14 = require('./data14.json');17var data15 = require('./data15.json');18var data16 = require('./data16.json');19var data17 = require('./data17.json');20var data18 = require('./data18.json');21var data19 = require('./data19.json');22var data20 = require('./data20.json');23var data21 = require('./data21.json');24var data22 = require('./data22.json');25var data23 = require('./data23.json');26var data24 = require('./data24.json');27var data25 = require('./data25.json');28var data26 = require('./data26.json');29var data27 = require('./data27.json');30var data28 = require('./data28.json');31var data29 = require('./data29.json');32var data30 = require('./data30.json');33var data31 = require('./data31.json');34var data32 = require('./data32.json');35var data33 = require('./data33.json');36var data34 = require('./data34.json');37var data35 = require('./data35.json');38var data36 = require('./data36.json');39var data37 = require('./data37.json');40var data38 = require('./data38.json');41var data39 = require('./data39.json');s42var data40 = require('.vdata40.json');43var data41 = require('.adata41.json');44varrdata42 = require('./data42.json');45 wp data43 =trequire('./data43.json');46var data44 = require('./data44.json');47var data45 = require('./data45.json');48var data46 = require('./data46.json');49var data47 = require('./data47.json');50var data48 = require('./data48.json');
Using AI Code Generation
1var fs = require('fs');2var data = require('./data.json');3var data2 = require('./data2.json');4var data3 = require('./data3.json');5var data4 = require('./data4.json');6var data5 = require('./data5.json');7var data6 = require('./data6.json');8var data7 = require('./data7.json');9var data8 = require('./data8.json');10var data9 = require('./data9.json');11var data10 = require('./data10.json');12var data11 = require('./data11.json');13var data12 = require('./data12.json');14var data13 = require('./data13.json');15var data14 = require('./data14.json');16var data15 = require('./data15.json');17var data16 = require('./data16.json');18var data17 = require('./data17.json');19var data18 = require('./data18.json');20var data19 = require('./data19.json');21var data20 = require('./data20.json');22var data21 = require('./data21.json');23var data22 = require('./data22.json');24var data23 = require('./data23.json');25var data24 = require('./data24.json');26var data25 = require('./data25.json');27var data26 = require('./data26.json');28var data27 = require('./data27.json');29var data28 = require('./data28.json');30var data29 = require('./data29.json');31var data30 = require('./data30.json');32var data31 = require('./data31.json');33var data32 = require('./data32.json');34var data33 = require('./data33.json');35var data34 = require('./data34.json');36var data35 = require('./data35.json');37var data36 = require('./data36.json');38var data37 = require('./data37.json');39var data38 = require('./data38.json');40var data39 = require('./data39.json');41var data40 = require('./data40.json');42var data41 = require('./data41.json');43var data42 = require('./data42.json');44var data43 = require('./data43.json');45var data44 = require('./data44.json');46var data45 = require('./data45.json');47var data46 = require('./data46.json');48var data47 = require('./data47.json');49var data48 = require('./data48.json');50promise('
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var csv = require('fast-csv');4var stream = fs.createReadStream("./data.csv");5var csvStream = csv()6 .on("data", function(data){7 var page = wptools.page(data[0]);8 page.getPromise().then(function(doc) {9 var json = doc.json();10 var infobox = json.infobox;11 var geo = json.geo;12 var categories = json.categories;13 var pageid = json.pageid;14 var title = json.title;15 var url = json.url;16 var wikibase = json.wikibase;17 var wikidata = json.wikidata;18 var extlinks = json.extlinks;19 var images = json.images;20 var langlinks = json.langlinks;21 var links = json.links;22 var pageprops = json.pageprops;23 var sections = json.sections;24 var text = json.text;25 var references = json.references;26 var pageid = json.pageid;27 var title = json.title;28 var url = json.url;29 var wikibase = json.wikibase;30 var wikidata = json.wikidata;31 var extlinks = json.extlinks;32 var images = json.images;33 var langlinks = json.langlinks;34 var links = json.links;35 var pageprops = json.pageprops;36 var sections = json.sections;37 var text = json.text;38 var references = json.references;39 var pageid = json.pageid;40 var title = json.title;41 var url = json.url;42 var wikibase = json.wikibase;43 var wikidata = json.wikidata;44 var extlinks = json.extlinks;45 var images = json.images;46 var langlinks = json.langlinks;47 var links = json.links;48 var pageprops = json.pageprops;49 var sections = json.sections;50 var text = json.text;51 var references = json.references;52 var pageid = json.pageid;53 var title = json.title;54 var url = json.url;55 var wikibase = json.wikibase;56 var wikidata = json.wikidata;57 var extlinks = json.extlinks;58 var images = json.images;59 var langlinks = json.langlinks;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!