Best JavaScript code snippet using wpt
promise3.js
Source:promise3.js
...17 // Promise/A+ 2.3.3.3.3 åªè½è°ç¨ä¸æ¬¡18 let thenCalledOrThrow;19 // èªå·±çå¾
èªå·±å®ææ¯é误çå®ç°ï¼ç¨ä¸ä¸ªç±»åé误ï¼ç»ææ promise Promise/A+ 2.3.120 if (promise2 === x) {21 return rejected(22 new TypeError("Chaining cycle detected for promise #<Promise>")23 );24 }25 // // PromiseA+2.3.226 // if (x instanceof Promise) {27 // //å¦æxçç¶æ没æç¡®å®ï¼é£ä¹å®æ¯æå¯è½è¢«ä¸ä¸ªthenableå³å®æç»ç¶æå¼ç28 // if (x.state === PENDING) {29 // x.then((value) => {30 // resolvePromise(promise2, value, resolve, rejected);31 // }, rejected);32 // } else {33 // x.then(resolve, rejected);34 // }35 // return;36 // }37 if ((x !== null && typeof x === "object" )|| typeof x === "function") {38 try {39 // 2.3.3.1 å 为x.thenæå¯è½æ¯ä¸ä¸ªgetterï¼è¿ç§æ
åµä¸å¤æ¬¡è¯»åå°±æå¯è½äº§çå¯ä½ç¨40 // ä¸è¦åæ x.thenï¼ç´æ¥ then.call å°±å¯ä»¥äº å 为 x.then ä¼å次åå¼ï¼Object.defineProperty Promise/A+ 2.3.3.341 // å³è¦å¤æå®çç±»åï¼åè¦è°ç¨å®ï¼è¿å°±æ¯ä¸¤æ¬¡è¯»å42 then = x.then;43 if (typeof then === "function") {44 then.call(45 x,46 (resolvedValue) => {47 if (thenCalledOrThrow) return; // 2.3.3.3.3 å³è¿ä¸å¤è°éæ§è¡å°±ä»¥è°çç»æ为å48 thenCalledOrThrow = true;49 // éå½è§£æçè¿ç¨ï¼å 为å¯è½ promise ä¸è¿æ promiseï¼ Promise/A+ 2.3.3.3.150 resolvePromise(promise2, resolvedValue, resolve, rejected);51 },52 (rejectedReason) => {53 if (thenCalledOrThrow) return; // 2.3.3.3.3 å³è¿ä¸å¤è°éæ§è¡å°±ä»¥è°çç»æ为å54 thenCalledOrThrow = true;55 rejected(rejectedReason);56 }57 );58 } else {59 //å¦æ x.then æ¯ä¸ªæ®éå¼å°±ç´æ¥è¿å resolve ä½ä¸ºç»æ Promise/A+ 2.3.3.460 resolve(x);61 }62 } catch (error) {63 if (thenCalledOrThrow) return; // 2.3.3.3.3 å³è¿ä¸å¤è°éæ§è¡å°±ä»¥è°çç»æ为å64 thenCalledOrThrow = true;65 rejected(error);66 }67 } else {68 //å¦æ x.then æ¯ä¸ªæ®éå¼å°±ç´æ¥è¿å resolve ä½ä¸ºç»æ Promise/A+ 2.3.3.469 resolve(x);70 }71}72class Promise {73 constructor(executor) {74 this.state = PENDING;75 this.value = undefined;76 this.reason = undefined;77 this.onResolvedCallbacks = []; // æåçåè°78 this.onRejectedCallbacks = []; // 失败çåè°79 let resolve = (value) => {80 // ======æ°å¢é»è¾======81 // å¦æ value æ¯ä¸ä¸ªpromiseï¼é£æ们çåºä¸åºè¯¥ä¹è¦å®ç°ä¸ä¸ªéå½è§£æ82 if(value instanceof Promise){83 // éå½è§£æ 84 return value.then(resolve,rejecte)85 }86 if (this.state === PENDING) {87 this.state = RESOLVED;88 this.value = value;89 // ä¾æ¬¡æ§è¡å½æ°90 for (let i = 0; i < this.onResolvedCallbacks.length; i++) {91 this.onResolvedCallbacks[i](value);92 }93 }94 };95 let rejecte = (reason) => {96 if (this.state === PENDING) {97 this.state = REJECTED;98 this.reason = reason;99 // ä¾æ¬¡æ§è¡å½æ°100 for (let i = 0; i < this.onRejectedCallbacks.length; i++) {101 this.onRejectedCallbacks[i](reason);102 }103 }104 };105 // èèå°æ§è¡executorçè¿ç¨ä¸æå¯è½åºéï¼æ们ç¨try/catchåç»å
èµ·æ¥ï¼å¹¶ä¸å¨åºéå以catchå°çå¼rejectæè¿ä¸ªPromise106 try {107 executor(resolve, rejecte);108 } catch (error) {109 rejecte(error);110 }111 }112 // then æ¹æ³ 并æ¥å两个åæ° onFulfilled,onRejected113 // then æ¹æ³å¯ä»¥è¿è¡é¾å¼è°ç¨ï¼ then å¯ä»¥å¨åä¸ä¸ªpromiseä¸å¤æ¬¡è¿è¡è°ç¨ PromiseA+2.2.6114 // then æ¹æ³å¿
é¡»returnä¸ä¸ªæ°çpromise PromiseA+2.2.7115 // å¼çç©¿é PromiseA+2.2.7116 // å¦æ onFullfilledæè
onRejected æåºå¼å¸¸ï¼åpromise2 ä¸å®ä¼æè¿ä¸ªå¼å¸¸ä½ä¸ºåæ°ä¼ éä¸å»117 // å¦æonFullfilled ä¸æ¯ä¸ä¸ªå½æ°ï¼å¹¶ä¸promise1 æ¯Fullfilledç¶æï¼åpromise2 onFullfilled åå¿
ç¶ä¼æpromise1 çå¼ä¼ éä¸å»118 // å¦æonRejected ä¸æ¯ä¸ä¸ªå½æ°ï¼å¹¶ä¸promise1 æ¯rejectedç¶æï¼åpromise2 onRejectedåå¿
ç¶ä¼æpromise1 çreasonä¼ éä¸å»119 // thençåæ°å¯ä»¥çç¥ï¼å³onFullfilledæè
onRejectedä¸æ¯å½æ°ï¼ä¸ä¾æ§å¯ä»¥å¨ä¸ä¸ä¸ªthenä¸è·åå°ä¹åè¿åçå¼ PromiseA+2.2.1120 // å¦æ onFullfilledæè
onRejected ï¼å¦æè¿åå¼ x æ¯ä¸ä¸ªpromise ï¼åä¼çè¿ä¸ªpromise æ§è¡å®ï¼è¥æååèµ°ä¸ä¸ä¸ªthençæåonFullfilledï¼è¥å¤±è´¥ï¼åèµ°ä¸ä¸ä¸ªthençonRejected, å¦ææåºå¼å¸¸åèµ°ä¸ä¸ä¸ªthenç失败121 // å¦æthen çè¿åå¼ x åpromiseæ¯åä¸ä¸ªå¼ç¨å¯¹è±¡ï¼åä¼å¨æ循ç¯å¼ç¨ï¼ä¼æåºå¼å¸¸ï¼æå¼å¸¸ä¼ éç»ä¸ä¸ä¸ªthenç失败çåè°ä¸ PromiseA+2.3.1122 then(onFulfilled, onRejected) {123 // å¼çç©¿é å¤æ 两个åæ°æ¯å¦ä¸ºå½æ° thené»è®¤åæ°æå¼å¾åä¼ æè
æå¼å¸¸124 onFulfilled =125 typeof onFulfilled === "function" ? onFulfilled : (value) => value;126 onRejected =127 typeof onRejected === "function"128 ? onRejected129 : (reason) => {130 throw reason;131 };132 // æ¯æ¬¡è°ç¨then é½è¿åä¸ä¸ªæ°çpromise对象 Promise/A+ 2.2.7133 let promise2 = new Promise((resolve, rejected) => {134 // ç¶æå为 æå135 if (this.state === RESOLVED) {136 // promiseA+3.1 2.2.4137 setTimeout(() => {138 try {139 // x å¯è½æ¯ä¸ä¸ªpromise140 let x = onFulfilled(this.value);141 resolvePromise(promise2, x, resolve, rejected);142 } catch (error) {143 rejected(error);144 }145 }, 0);146 }147 // ç¶æå为 失败148 if (this.state === REJECTED) {149 // promiseA+3.1 2.2.4150 setTimeout(() => {151 try {152 // x å¯è½æ¯ä¸ä¸ªpromise153 let x = onRejected(this.reason);154 resolvePromise(promise2, x, resolve, rejected);155 } catch (error) {156 rejected(error);157 }158 }, 0);159 }160 // ç¶ææ¯pending åå°onFulfilled,onRejected åæ¾å°å¯¹åºçéåä¸,çå¾
ç¶æç¡®å®ä¹åï¼åå»ä¾æ¬¡æ§è¡161 // å¦æå¤é¨è°ç¨ 为éäº1ç§æè¿è¡resolveæè
rejectedï¼åç¶æå¼å§ä¸ºpendingï¼ä¸ç§ä¹åï¼å为resolved æè
rejected162 if (this.state === PENDING) {163 this.onResolvedCallbacks.push(() => {164 setTimeout(() => {165 try {166 // x å¯è½æ¯ä¸ä¸ªpromise167 let x = onFulfilled(this.value);168 resolvePromise(promise2, x, resolve, rejected);169 } catch (error) {170 rejected(error);171 }172 }, 0);173 });174 this.onRejectedCallbacks.push(() => {175 // promiseA+3.1 2.2.4176 setTimeout(() => {177 try {178 // x å¯è½æ¯ä¸ä¸ªpromise179 let x = onRejected(this.reason);180 resolvePromise(promise2, x, resolve, rejected);181 } catch (error) {182 rejected(error);183 }184 }, 0);185 });186 }187 });188 return promise2;189 }190 // resolve æä¸ä¸ªçå¾
çåè½191 static resolve(data){192 if(data instanceof Promise) return data;193 return new Promise((resolve,reject)=>{194 if(data&& data.then&& typeof data.then ==='function'){195 data.then(resolve,reject)196 }else{...
promiseDemo.js
Source:promiseDemo.js
1// // ä¸ä¸ªç¶æï¼PENDINGãFULFILLEDãREJECTED2// const PENDING = "PENDING";3// const FULFILLED = "FULFILLED";4// const REJECTED = "REJECTED";5// class Promise {6// constructor(executor) {7// // é»è®¤ç¶æ为 PENDING8// this.status = PENDING;9// // åæ¾æåç¶æçå¼ï¼é»è®¤ä¸º undefined10// this.value = undefined;11// // åæ¾å¤±è´¥ç¶æçå¼ï¼é»è®¤ä¸º undefined12// this.reason = undefined;13// // è°ç¨æ¤æ¹æ³å°±æ¯æå14// let resolve = (value) => {15// // ç¶æ为 PENDING æ¶æå¯ä»¥æ´æ°ç¶æï¼é²æ¢ executor ä¸è°ç¨äºä¸¤æ¬¡ resovle/reject æ¹æ³16// if (this.status === PENDING) {17// this.status = FULFILLED;18// this.value = value;19// }20// };21// // è°ç¨æ¤æ¹æ³å°±æ¯å¤±è´¥22// let reject = (reason) => {23// // ç¶æ为 PENDING æ¶æå¯ä»¥æ´æ°ç¶æï¼é²æ¢ executor ä¸è°ç¨äºä¸¤æ¬¡ resovle/reject æ¹æ³24// if (this.status === PENDING) {25// this.status = REJECTED;26// this.reason = reason;27// }28// };29// try {30// // ç«å³æ§è¡ï¼å° resolve å reject å½æ°ä¼ ç»ä½¿ç¨è
31// executor(resolve, reject);32// } catch (error) {33// // åçå¼å¸¸æ¶æ§è¡å¤±è´¥é»è¾34// reject(error);35// }36// }37// // å
å«ä¸ä¸ª then æ¹æ³ï¼å¹¶æ¥æ¶ä¸¤ä¸ªåæ° onFulfilledãonRejected38// then(onFulfilled, onRejected) {39// console.log(this.status,1111);40// if (this.status === FULFILLED) {41// onFulfilled(this.value);42// }43// if (this.status === REJECTED) {44// onRejected(this.reason);45// }46// }47// }48// const PENDING = "PENDING";49// const FULFILLED = "FULFILLED";50// const REJECTED = "REJECTED";51// class Promise {52// constructor(executor) {53// this.status = PENDING;54// this.value = undefined;55// this.reason = undefined;56// // åæ¾æåçåè°57// this.onResolvedCallbacks = [];58// // åæ¾å¤±è´¥çåè°59// this.onRejectedCallbacks = [];60// let resolve = (value) => {61// console.log(this.onResolvedCallbacks, this.onRejectedCallbacks);62// if (this.status === PENDING) {63// this.status = FULFILLED;64// this.value = value;65// // ä¾æ¬¡å°å¯¹åºçå½æ°æ§è¡66// this.onResolvedCallbacks.forEach((fn) => fn());67// }68// };69// let reject = (reason) => {70// if (this.status === PENDING) {71// this.status = REJECTED;72// this.reason = reason;73// // ä¾æ¬¡å°å¯¹åºçå½æ°æ§è¡74// this.onRejectedCallbacks.forEach((fn) => fn());75// }76// };77// try {78// executor(resolve, reject);79// } catch (error) {80// reject(error);81// }82// }83// then(onFulfilled, onRejected) {84// if (this.status === FULFILLED) {85// onFulfilled(this.value);86// }87// if (this.status === REJECTED) {88// onRejected(this.reason);89// }90// if (this.status === PENDING) {91// // å¦æpromiseçç¶ææ¯ pendingï¼éè¦å° onFulfilled å onRejected å½æ°åæ¾èµ·æ¥ï¼çå¾
ç¶æç¡®å®åï¼åä¾æ¬¡å°å¯¹åºçå½æ°æ§è¡92// this.onResolvedCallbacks.push(() => {93// onFulfilled(this.value);94// });95// // å¦æpromiseçç¶ææ¯ pendingï¼éè¦å° onFulfilled å onRejected å½æ°åæ¾èµ·æ¥ï¼çå¾
ç¶æç¡®å®åï¼åä¾æ¬¡å°å¯¹åºçå½æ°æ§è¡96// this.onRejectedCallbacks.push(() => {97// onRejected(this.reason);98// });99// }100// }101// }102const PENDING = "PENDING";103const FULFILLED = "FULFILLED";104const REJECTED = "REJECTED";105const resolvePromise = (promise2, x, resolve, reject) => {106 // èªå·±çå¾
èªå·±å®ææ¯é误çå®ç°ï¼ç¨ä¸ä¸ªç±»åé误ï¼ç»ææ promise Promise/A+ 2.3.1107 if (promise2 === x) {108 return reject(109 new TypeError("Chaining cycle detected for promise #<Promise>")110 );111 }112 // Promise/A+ 2.3.3.3.3 åªè½è°ç¨ä¸æ¬¡113 let called;114 // åç»çæ¡ä»¶è¦ä¸¥æ ¼å¤æ ä¿è¯ä»£ç è½åå«çåºä¸èµ·ä½¿ç¨115 if ((typeof x === "object" && x != null) || typeof x === "function") {116 try {117 // 为äºå¤æ resolve è¿çå°±ä¸ç¨å reject äºï¼æ¯å¦ reject å resolve åæ¶è°ç¨çæ¶åï¼ Promise/A+ 2.3.3.1118 let then = x.then;119 if (typeof then === "function") {120 // ä¸è¦åæ x.thenï¼ç´æ¥ then.call å°±å¯ä»¥äº å 为 x.then ä¼å次åå¼ï¼Object.defineProperty Promise/A+ 2.3.3.3121 then.call(122 x,123 (y) => {124 // æ ¹æ® promise çç¶æå³å®æ¯æåè¿æ¯å¤±è´¥125 if (called) return;126 called = true;127 // éå½è§£æçè¿ç¨ï¼å 为å¯è½ promise ä¸è¿æ promiseï¼ Promise/A+ 2.3.3.3.1128 resolvePromise(promise2, y, resolve, reject);129 },130 (r) => {131 // åªè¦å¤±è´¥å°±å¤±è´¥ Promise/A+ 2.3.3.3.2132 if (called) return;133 called = true;134 reject(r);135 }136 );137 } else {138 // å¦æ x.then æ¯ä¸ªæ®éå¼å°±ç´æ¥è¿å resolve ä½ä¸ºç»æ Promise/A+ 2.3.3.4139 resolve(x);140 }141 } catch (e) {142 // Promise/A+ 2.3.3.2143 if (called) return;144 called = true;145 reject(e);146 }147 } else {148 // å¦æ x æ¯ä¸ªæ®éå¼å°±ç´æ¥è¿å resolve ä½ä¸ºç»æ Promise/A+ 2.3.4149 resolve(x);150 }151};152class Promise {153 constructor(executor) {154 this.status = PENDING;155 this.value = undefined;156 this.reason = undefined;157 this.onResolvedCallbacks = [];158 this.onRejectedCallbacks = [];159 let resolve = (value) => {160 // ======æ°å¢é»è¾======161 // promise.resolve æ¯å
·å¤çå¾
åè½çã162 // å¦æ value æ¯ä¸ä¸ªpromiseï¼é£æ们çåºä¸åºè¯¥ä¹è¦å®ç°ä¸ä¸ªéå½è§£æ163 if (value instanceof Promise) {164 // éå½è§£æ165 return value.then(resolve, reject);166 }167 if (this.status === PENDING) {168 this.status = FULFILLED;169 this.value = value;170 this.onResolvedCallbacks.forEach((fn) => fn());171 }172 };173 let reject = (reason) => {174 if (this.status === PENDING) {175 this.status = REJECTED;176 this.reason = reason;177 this.onRejectedCallbacks.forEach((fn) => fn());178 }179 };180 try {181 executor(resolve, reject);182 } catch (error) {183 reject(error);184 }185 }186 then(onFulfilled, onRejected) {187 //è§£å³ onFufilledï¼onRejected 没æä¼ å¼çé®é¢188 //Promise/A+ 2.2.1 / Promise/A+ 2.2.5 / Promise/A+ 2.2.7.3 / Promise/A+ 2.2.7.4189 onFulfilled = typeof onFulfilled === "function" ? onFulfilled : (v) => v;190 //å 为é误çå¼è¦è®©åé¢è®¿é®å°ï¼æ以è¿éä¹è¦è·åºä¸ªé误ï¼ä¸ç¶ä¼å¨ä¹å then ç resolve ä¸æè·191 onRejected =192 typeof onRejected === "function"193 ? onRejected194 : (err) => {195 throw err;196 };197 // æ¯æ¬¡è°ç¨ then é½è¿åä¸ä¸ªæ°ç promise Promise/A+ 2.2.7198 let promise2 = new Promise((resolve, reject) => {199 if (this.status === FULFILLED) {200 //Promise/A+ 2.2.2201 //Promise/A+ 2.2.4 --- setTimeout202 setTimeout(() => {203 try {204 //Promise/A+ 2.2.7.1205 let x = onFulfilled(this.value);206 // xå¯è½æ¯ä¸ä¸ªproimise207 resolvePromise(promise2, x, resolve, reject);208 } catch (e) {209 //Promise/A+ 2.2.7.2210 reject(e);211 }212 }, 0);213 }214 if (this.status === REJECTED) {215 //Promise/A+ 2.2.3216 setTimeout(() => {217 try {218 let x = onRejected(this.reason);219 resolvePromise(promise2, x, resolve, reject);220 } catch (e) {221 reject(e);222 }223 }, 0);224 }225 if (this.status === PENDING) {226 this.onResolvedCallbacks.push(() => {227 setTimeout(() => {228 try {229 let x = onFulfilled(this.value);230 resolvePromise(promise2, x, resolve, reject);231 } catch (e) {232 reject(e);233 }234 }, 0);235 });236 this.onRejectedCallbacks.push(() => {237 setTimeout(() => {238 try {239 let x = onRejected(this.reason);240 resolvePromise(promise2, x, resolve, reject);241 } catch (e) {242 reject(e);243 }244 }, 0);245 });246 }247 });248 return promise2;249 }250 static resolve(data) {251 return new Promise((resolve, reject) => {252 resolve(data);253 });254 }255 static reject(reason) {256 return new Promise((resolve, reject) => {257 reject(reason);258 });259 }260}261Promise.prototype.catch = function (errCallback) {262 return this.then(null, errCallback);263};264Promise.prototype.finally = function (callback) {265 return this.then(266 (value) => {267 return Promise.resolve(callback()).then(() => value);268 },269 (reason) => {270 return Promise.resolve(callback()).then(() => {271 throw reason;272 });273 }274 );275};276Promise.all = function (values) {277 if (!Array.isArray(values)) {278 const type = typeof values;279 return new TypeError(`TypeError: ${type} ${values} is not iterable`);280 }281 return new Promise((resolve, reject) => {282 let resultArr = [];283 let orderIndex = 0;284 const processResultByKey = (value, index) => {285 resultArr[index] = value;286 if (++orderIndex === values.length) {287 resolve(resultArr);288 }289 };290 for (let i = 0; i < values.length; i++) {291 let value = values[i];292 if (value && typeof value.then === "function") {293 value.then((value) => {294 processResultByKey(value, i);295 }, reject);296 } else {297 processResultByKey(value, i);298 }299 }300 });301};302Promise.race = function (promises) {303 return new Promise((resolve, reject) => {304 // ä¸èµ·æ§è¡å°±æ¯for循ç¯305 for (let i = 0; i < promises.length; i++) {306 let val = promises[i];307 if (val && typeof val.then === "function") {308 val.then(resolve, reject);309 } else {310 // æ®éå¼311 resolve(val);312 }313 }314 });315};316export default () => {317 const promise = new Promise((resolve, reject) => {318 setTimeout(() => {319 resolve("æå");320 }, 1000);321 })322 .then(323 (data) => {324 console.log("success", data);325 return 3;326 },327 (err) => {328 console.log("faild", err);329 }330 )331 .then((num) => {332 console.log("success again", num);333 });...
day2.js
Source:day2.js
1const PENDING = 'PENDING';2const FULFILLED = 'FULFILLED';3const REJECTED = 'REJECTED';4class MyPromise {5 constructor(executor) {6 this._value = undefined;7 this._reason = undefined;8 this._status = PENDING;9 this._onResolvedCbs = [];10 this._onRejectedCbs = [];11 try {12 executor(this._resolve.bind(this), this._reject.bind(this));13 } catch (error) {14 this._reject(error);15 }16 }17 _resolve(value) {18 if (this._status === PENDING) {19 setTimeout(() => {20 this._status = FULFILLED;21 this._value = value;22 while (this._onResolvedCbs.length) {23 const fn = this._onResolvedCbs.shift();24 fn(this._value);25 }26 }, 0);27 }28 }29 _reject(reason) {30 if (this._status === PENDING) {31 setTimeout(() => {32 this._status = REJECTED;33 this._reason = reason;34 while (this._onRejectedCbs.length) {35 const fn = this._onRejectedCbs.shift();36 fn(this._reason);37 }38 }, 0);39 }40 }41 then(onResolved, onRejected) {42 onResolved = typeof onResolved === 'function' ? onResolved : (v) => v;43 onRejected =44 typeof onRejected === 'function'45 ? onRejected46 : (r) => {47 throw r;48 };49 if (this._status === FULFILLED) {50 const p2 = new MyPromise((resolve, reject) => {51 setTimeout(() => {52 try {53 const x = onResolved(this._value);54 MyPromise.resolvePromise(p2, x, resolve, reject);55 } catch (error) {56 reject(error);57 }58 }, 0);59 });60 return p2;61 }62 if (this._status === REJECTED) {63 const p2 = new MyPromise((resolve, reject) => {64 setTimeout(() => {65 try {66 const x = onRejected(this._reason);67 MyPromise.resolvePromise(p2, x, resolve, reject);68 } catch (error) {69 reject(error);70 }71 }, 0);72 });73 return p2;74 }75 if (this._status === PENDING) {76 const p2 = new MyPromise((resolve, reject) => {77 this._onResolvedCbs.push((value) => {78 try {79 const x = onResolved(value);80 MyPromise.resolvePromise(p2, x, resolve, reject);81 } catch (error) {82 reject(error);83 }84 });85 this._onRejectedCbs.push((r) => {86 try {87 const x = onRejected(r);88 MyPromise.resolvePromise(p2, x, resolve, reject);89 } catch (error) {90 reject(error);91 }92 });93 });94 return p2;95 }96 }97 static resolvePromise(promise, x, resolve, reject) {98 if (promise === x) {99 return reject(new TypeError());100 }101 if (x instanceof MyPromise) {102 if (x._status === PENDING) {103 x.then(function (y) {104 MyPromise.resolvePromise(promise, y, resolve, reject);105 }, reject);106 } else {107 x.then(resolve, reject);108 }109 } else if (110 x !== null &&111 (typeof x === 'function' || typeof x === 'object')112 ) {113 let called = false;114 try {115 const then = x.then;116 if (typeof then === 'function') {117 then.call(118 x,119 function (y) {120 if (called) return;121 called = true;122 MyPromise.resolvePromise(promise, y, resolve, reject);123 },124 function (r) {125 if (called) return;126 called = true;127 reject(r);128 }129 );130 } else {131 if (called) return;132 called = true;133 resolve(x);134 }135 } catch (error) {136 if (called) return;137 called = true;138 reject(error);139 }140 } else {141 resolve(x);142 }143 }144}...
test.js
Source:test.js
1/* eslint-env mocha */2const chai = require('chai')3const chaiAsPromised = require('chai-as-promised')4const path = require('path')5const decache = require('decache')6const maildrop = require(path.join(__dirname, '..', 'src', 'maildrop'))7chai.use(chaiAsPromised)8chai.should()9const mailBox = 'john'10describe('suggestion()', () => {11 it('should be fulfilled with a string without error', () => {12 return maildrop.suggestion()13 .should.eventually.be.fulfilled.and.be.a('string')14 })15})16describe('blocked()', () => {17 it('should be fulfilled with a number without error', () => {18 return maildrop.blocked()19 .should.eventually.be.fulfilled.and.be.a('number')20 })21})22describe('getInbox(mailBox)', () => {23 it('should be fulfilled with an array without error', () => {24 return maildrop.getInbox(mailBox)25 .should.eventually.be.fulfilled.and.be.an('array')26 })27})28describe('getInbox()', () => {29 it('should be rejected with error', () => {30 return maildrop.getInbox()31 .should.eventually.be.rejected.and.be.an('error')32 })33})34describe('getEmail(mailBox, mailBox)', () => {35 it('should be fulfilled with an object without error', () => {36 return maildrop.getEmail(mailBox, mailBox)37 .should.eventually.be.fulfilled.and.be.an('object')38 })39})40describe('getEmail()', () => {41 it('should be rejected with error', () => {42 return maildrop.getEmail()43 .should.eventually.be.rejected.and.be.an('error')44 })45})46describe('deleteEmail(mailBox, mailBox)', () => {47 it('should be fulfilled with an object without error', () => {48 return maildrop.deleteEmail(mailBox, mailBox)49 .should.eventually.be.fulfilled.and.be.an('object')50 })51})52describe('deleteEmail()', () => {53 it('should be rejected with error', () => {54 return maildrop.deleteEmail()55 .should.eventually.be.rejected.and.be.an('error')56 })57})58// To test the error handling we shall set MAILDROP_URL env and re-require59// the maildrop module60process.env.MAILDROP_URL = 'http://localhost100'61decache(path.join(__dirname, '..', 'src', 'maildrop'))62const maildropError = require(path.join(__dirname, '..', 'src', 'maildrop'))63describe('suggestion() - error handling', () => {64 it('should be rejected with an error', () => {65 return maildropError.suggestion()66 .should.eventually.be.rejected.and.be.an('error')67 })68})69describe('blocked() - error handling', () => {70 it('should be rejected with an error', () => {71 return maildropError.blocked()72 .should.eventually.be.rejected.and.be.an('error')73 })74})75describe('getInbox(mailBox) - error handling', () => {76 it('should be rejected with error', () => {77 return maildropError.getInbox(mailBox)78 .should.eventually.be.rejected.and.be.an('error')79 })80})81describe('getEmail(mailBox, mailBox) - error handling', () => {82 it('should be rejected with error', () => {83 return maildropError.getEmail(mailBox, mailBox)84 .should.eventually.be.rejected.and.be.an('error')85 })86})87describe('deleteEmail(mailBox, mailBox) - error handling', () => {88 it('should be rejected with error', () => {89 return maildropError.deleteEmail(mailBox, mailBox)90 .should.eventually.be.rejected.and.be.an('error')91 })...
env.js
Source:env.js
1const Get = {2 API_URL : process.env.REACT_APP_API_URL,3 Users_Pending : 'GET_ALL_USERS_PENDING',4 Users_Fulfilled : 'GET_ALL_USERS_FULFILLED',5 Users_Rejected : 'GET_ALL_USERS_REJECTED',6 User_DetailFulfilled : 'GET_DETAIL_USER_FULFILLED',7 User_DetailRejected : 'GET_DETAIL_USER_REJECTED',8 9 Products_Pending : 'GET_ALL_PRODUCTS_PENDING',10 Products_Fulfilled : 'GET_ALL_PRODUCTS_FULFILLED',11 Products_Rejected : 'GET_ALL_PRODUCTS_REJECTED',12 Product_DetailPending : 'GET_DETAIL_PRODUCT_PENDING',13 Product_DetailFulfilled : 'GET_DETAIL_PRODUCT_FULFILLED',14 Product_DetailRejected : 'GET_DETAIL_PRODUCT_REJECTED',15 16 Cart : 'GET_CART',17 Add_Qty_Cart : 'ADD_QTY_CART',18 Min_Qty_Cart : 'MIN_QTY_CART',19 Delete_Cart : 'DELETE_CART',20 Transaction_Pending : 'GET_TRANSACTIONS_PENDING',21 Transaction_Fulfilled : 'GET_TRANSACTIONS_FULFILLED',22 Transaction_Rejected : 'GET_TRANSACTIONS_REJECTED',23 DetailTransaction_Pending : 'GET_DETAIL_TRANSACTIONS_PENDING',24 DetailTransaction_Fulfilled : 'GET_DETAIL_TRANSACTIONS_FULFILLED',25 DetailTransaction_Rejected : 'GET_DETAIL_TRANSACTIONS_REJECTED',26}...
Using AI Code Generation
1var wptools = require('wptools');2wiki.get(function(err, response) {3 console.log(response);4});5var wptools = require('wptools');6var wiki = wptools.page('Barack Obama');7wiki.get(function(err, response) {8 console.log(response);9});
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Barack Obama');3wp.get(function(err, data) {4 console.log(data);5});6{ error: 'Page not found' }7var wptools = require('wptools');8var wp = wptools.page('Barack Obama');9wp.get(function(err, data) {10 if (err) {11 console.log(err);12 }13});14{ error: 'Page not found' }15var wptools = require('wptools');16var wp = wptools.page('Barack Obama');17wp.get().then(function(data) {18 console.log(data);19});20{ error: 'Page not found' }21var wptools = require('wptools');22var wp = wptools.page('Barack Obama');23wp.get().then(function(data) {24 console.log(data);25}, function(err) {26 console.log(err);27});28{ error: 'Page not found' }29var wptools = require('wptools');30var wp = wptools.page('Barack Obama');31wp.get().then(function(data) {32 console.log(data);33}).catch(function(err) {34 console.log(err);35});36{ error: 'Page not found' }37var wptools = require('wptools');38var wp = wptools.page('Barack Obama');39wp.get().then(function(data) {40 console.log(data);41}).catch
Using AI Code Generation
1const wptools = require('wptools');2const wiki = wptools.page('Gandhi');3wiki.reject((err, doc) => {4 console.log(doc);5});6const wptools = require('wptools');7const wiki = wptools.page('Gandhi');8wiki.get((err, doc) => {9 console.log(doc);10});11const wptools = require('wptools');12const wiki = wptools.page('Gandhi');13wiki.then((doc) => {14 console.log(doc);15});16const wptools = require('wptools');17const wiki = wptools.page('Gandhi');18wiki.catch((err) => {19 console.log(err);20});21const wptools = require('wptools');22const wiki = wptools.page('Gandhi');23wiki.finally(() => {24 console.log('finally');25});26const wptools = require('wptools');27const wiki = wptools.page('Gandhi');28const doc = await wiki.get();29console.log(doc);30const wptools = require('wptools');31const wiki = wptools.page('Gandhi');32try {33 const doc = await wiki.get();34 console.log(doc);35} catch (err) {36 console.log(err);37}38const wptools = require('wptools');39const wiki = wptools.page('Gandhi');40const doc = await wiki.get();41console.log(doc);42const wptools = require('wptools');43const wiki = wptools.page('Gandhi');44try {45 const doc = await wiki.get();46 console.log(doc);47} catch (err) {48 console.log(err);49}50const wptools = require('wptools');51const wiki = wptools.page('Gandhi');52try {
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest('www.webpagetest.org', options);5}, function(err, data) {6 if (err) {7 console.log('Error: ' + err);8 } else {9 console.log('Test status: ' + data.statusText);10 console.log('Test ID: ' + data.data.testId);11 console.log('Test URL: ' + data.data.summary);12 }13});14wpt.getTestResults('150205_3H_EE', function(err, data) {15 if (err) {16 console.log('Error: ' + err);17 } else {18 console.log('Test status: ' + data.statusText);19 console.log('Test ID: ' + data.data.testId);20 console.log('Test URL: ' + data.data.summary);21 }22});23wpt.getLocations(function(err, data) {24 if (err) {25 console.log('Error: ' + err);26 } else {27 console.log('Test status: ' + data.statusText);28 console.log('Test ID: ' + data.data.testId);29 console.log('Test URL: ' + data.data.summary);30 }31});32wpt.getTesters(function(err, data) {33 if (err) {34 console.log('Error: ' + err);35 } else {36 console.log('Test status: ' + data.statusText);37 console.log('Test ID: ' + data.data.testId);38 console.log('Test URL: ' + data.data.summary);39 }40});41wpt.getTesters(function(err, data) {42 if (err) {43 console.log('Error: ' + err);44 } else {45 console.log('Test status: ' + data.statusText);46 console.log('Test ID
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!!