Best JavaScript code snippet using wpt
3.2.6.js
Source:3.2.6.js
1"use strict";2var assert = require("assert");3var testFulfilled = require("./helpers/testThreeCases").testFulfilled;4var testRejected = require("./helpers/testThreeCases").testRejected;5var adapter = global.adapter;6var fulfilled = adapter.fulfilled;7var rejected = adapter.rejected;8var pending = adapter.pending;9var dummy = { dummy: "dummy" }; // we fulfill or reject with this when we don't intend to test against it10var sentinel = { sentinel: "sentinel" }; // a sentinel fulfillment value to test for with strict equality11var other = { other: "other" }; // a value we don't want to be strict equal to12describe("3.2.6: `then` must return a promise: `promise2 = promise1.then(onFulfilled, onRejected)`", function () {13 specify("is a promise", function () {14 var promise1 = pending().promise;15 var promise2 = promise1.then();16 assert(typeof promise2 === "object" || typeof promise2 === "function");17 assert.notStrictEqual(promise2, null);18 assert.strictEqual(typeof promise2.then, "function");19 });20 describe("3.2.6.1: If either `onFulfilled` or `onRejected` returns a value that is not a promise, `promise2` " +21 "must be fulfilled with that value.", function () {22 function testValue(expectedValue, stringRepresentation) {23 describe("The value is " + stringRepresentation, function () {24 testFulfilled(dummy, function (promise1, done) {25 var promise2 = promise1.then(function onFulfilled() {26 return expectedValue;27 });28 promise2.then(function onPromise2Fulfilled(actualValue) {29 assert.strictEqual(actualValue, expectedValue);30 done();31 });32 });33 testRejected(dummy, function (promise1, done) {34 var promise2 = promise1.then(null, function onRejected() {35 return expectedValue;36 });37 promise2.then(function onPromise2Fulfilled(actualValue) {38 assert.strictEqual(actualValue, expectedValue);39 done();40 });41 });42 });43 }44 testValue(undefined, "`undefined`");45 testValue(null, "`null`");46 testValue(false, "`false`");47 testValue(0, "`0`");48 testValue(new Error(), "an error");49 testValue(new Date(), "a date");50 testValue({}, "an object");51 testValue({ then: 5 }, "an object with a non-function `then` property");52 });53 describe("3.2.6.2: If either `onFulfilled` or `onRejected` throws an exception, `promise2` " +54 "must be rejected with the thrown exception as the reason.", function () {55 function testReason(expectedReason, stringRepresentation) {56 describe("The reason is " + stringRepresentation, function () {57 testFulfilled(dummy, function (promise1, done) {58 var promise2 = promise1.then(function onFulfilled() {59 throw expectedReason;60 });61 promise2.then(null, function onPromise2Rejected(actualReason) {62 assert.strictEqual(actualReason, expectedReason);63 done();64 });65 });66 testRejected(dummy, function (promise1, done) {67 var promise2 = promise1.then(null, function onRejected() {68 throw expectedReason;69 });70 promise2.then(null, function onPromise2Rejected(actualReason) {71 assert.strictEqual(actualReason, expectedReason);72 done();73 });74 });75 });76 }77 testReason(undefined, "`undefined`");78 testReason(null, "`null`");79 testReason(false, "`false`");80 testReason(0, "`0`");81 testReason(new Error(), "an error");82 testReason(new Date(), "a date");83 testReason({}, "an object");84 testReason({ then: function () { } }, "a promise-alike");85 testReason(fulfilled(dummy), "a fulfilled promise");86 var promise = rejected(dummy); promise.caught(function(){});87 testReason(promise, "a rejected promise");88 });89 describe("3.2.6.3: If either `onFulfilled` or `onRejected` returns a promise (call it `returnedPromise`), " +90 "`promise2` must assume the state of `returnedPromise`", function () {91 describe("3.2.6.3.1: If `returnedPromise` is pending, `promise2` must remain pending until `returnedPromise` " +92 "is fulfilled or rejected.", function () {93 testFulfilled(dummy, function (promise1, done) {94 var wasFulfilled = false;95 var wasRejected = false;96 var promise2 = promise1.then(function onFulfilled() {97 var returnedPromise = pending().promise;98 return returnedPromise;99 });100 promise2.then(101 function onPromise2Fulfilled() {102 wasFulfilled = true;103 },104 function onPromise2Rejected() {105 wasRejected = true;106 }107 );108 setTimeout(function () {109 assert.strictEqual(wasFulfilled, false);110 assert.strictEqual(wasRejected, false);111 done();112 }, 100);113 });114 testRejected(dummy, function (promise1, done) {115 var wasFulfilled = false;116 var wasRejected = false;117 var promise2 = promise1.then(null, function onRejected() {118 var returnedPromise = pending().promise;119 return returnedPromise;120 });121 promise2.then(122 function onPromise2Fulfilled() {123 wasFulfilled = true;124 },125 function onPromise2Rejected() {126 wasRejected = true;127 }128 );129 setTimeout(function () {130 assert.strictEqual(wasFulfilled, false);131 assert.strictEqual(wasRejected, false);132 done();133 }, 100);134 });135 });136 describe("3.2.6.3.2: If/when `returnedPromise` is fulfilled, `promise2` must be fulfilled with the same value.",137 function () {138 describe("`promise1` is fulfilled, and `returnedPromise` is:", function () {139 testFulfilled(sentinel, function (returnedPromise, done) {140 var promise1 = fulfilled(dummy);141 var promise2 = promise1.then(function onFulfilled() {142 return returnedPromise;143 });144 promise2.then(function onPromise2Fulfilled(value) {145 assert.strictEqual(value, sentinel);146 done();147 });148 });149 specify("a pseudo-promise", function (done) {150 var promise1 = fulfilled(dummy);151 var promise2 = promise1.then(function onFulfilled() {152 return {153 then: function (f) { f(sentinel); }154 };155 });156 promise2.then(function onPromise2Fulfilled(value) {157 assert.strictEqual(value, sentinel);158 done();159 });160 });161 });162 describe("`promise1` is rejected, and `returnedPromise` is:", function () {163 testFulfilled(sentinel, function (returnedPromise, done) {164 var promise1 = rejected(dummy);165 var promise2 = promise1.then(null, function onRejected() {166 return returnedPromise;167 });168 promise2.then(function onPromise2Fulfilled(value) {169 assert.strictEqual(value, sentinel);170 done();171 });172 });173 specify("a pseudo-promise", function (done) {174 var promise1 = rejected(dummy);175 var promise2 = promise1.then(null, function onRejected() {176 return {177 then: function (f) { f(sentinel); }178 };179 });180 promise2.then(function onPromise2Fulfilled(value) {181 assert.strictEqual(value, sentinel);182 done();183 });184 });185 });186 });187 describe("3.2.6.3.3: If/when `returnedPromise` is rejected, `promise2` must be rejected with the same reason.",188 function () {189 describe("`promise1` is fulfilled, and `returnedPromise` is:", function () {190 testRejected(sentinel, function (returnedPromise, done) {191 var promise1 = fulfilled(dummy);192 var promise2 = promise1.then(function onFulfilled() {193 return returnedPromise;194 });195 promise2.then(null, function onPromise2Rejected(reason) {196 assert.strictEqual(reason, sentinel);197 done();198 });199 });200 specify("a pseudo-promise", function (done) {201 var promise1 = fulfilled(dummy);202 var promise2 = promise1.then(function onFulfilled() {203 return {204 then: function (f, r) { r(sentinel); }205 };206 });207 promise2.then(null, function onPromise2Rejected(reason) {208 assert.strictEqual(reason, sentinel);209 done();210 });211 });212 });213 describe("`promise1` is rejected, and `returnedPromise` is:", function () {214 testRejected(sentinel, function (returnedPromise, done) {215 var promise1 = rejected(dummy);216 var promise2 = promise1.then(null, function onRejected() {217 return returnedPromise;218 });219 promise2.then(null, function onPromise2Rejected(reason) {220 assert.strictEqual(reason, sentinel);221 done();222 });223 });224 specify("a pseudo-promise", function (done) {225 var promise1 = rejected(dummy);226 var promise2 = promise1.then(null, function onRejected() {227 return {228 then: function (f, r) { r(sentinel); }229 };230 });231 promise2.then(null, function onPromise2Rejected(reason) {232 assert.strictEqual(reason, sentinel);233 done();234 });235 });236 });237 });238 });239 describe("3.2.6.4: If `onFulfilled` is not a function and `promise1` is fulfilled, `promise2` must be fulfilled " +240 "with the same value.", function () {241 function testNonFunction(nonFunction, stringRepresentation) {242 describe("`onFulfilled` is " + stringRepresentation, function () {243 testFulfilled(sentinel, function (promise1, done) {244 var promise2 = promise1.then(nonFunction);245 promise2.then(function onPromise2Fulfilled(value) {246 assert.strictEqual(value, sentinel);247 done();248 });249 });250 });251 }252 testNonFunction(undefined, "`undefined`");253 testNonFunction(null, "`null`");254 testNonFunction(false, "`false`");255 testNonFunction(5, "`5`");256 testNonFunction({}, "an object");257 testNonFunction([function () { return other; }], "an array containing a function");258 });259 describe("3.2.6.5: If `onRejected` is not a function and `promise1` is rejected, `promise2` must be rejected " +260 "with the same reason.", function () {261 function testNonFunction(nonFunction, stringRepresentation) {262 describe("`onRejected` is " + stringRepresentation, function () {263 testRejected(sentinel, function (promise1, done) {264 var promise2 = promise1.then(null, nonFunction);265 promise2.then(null, function onPromise2Rejected(reason) {266 assert.strictEqual(reason, sentinel);267 done();268 });269 });270 });271 }272 testNonFunction(undefined, "`undefined`");273 testNonFunction(null, "`null`");274 testNonFunction(false, "`false`");275 testNonFunction(5, "`5`");276 testNonFunction({}, "an object");277 testNonFunction([function () { return other; }], "an array containing a function");278 });...
1.promise.js
Source:1.promise.js
1const PENDING = "pending";2const FULFILLED = "fulfilled";3const REJECTED = "rejected";4function Promise2(executor) {5 const self = this;6 self.status = PENDING;7 self.onResolveCallbacks = [];8 self.onRejectCallbacks = [];9 function resolve(value) {10 if (value instanceof Promise2) {11 return value.then(resolve, reject);12 }13 setTimeout(() => {14 if (self.status === PENDING) {15 self.status = FULFILLED;16 self.value = value;17 self.onResolveCallbacks.forEach(cb => cb(self.value));18 }19 });20 }21 function reject(reason) {22 setTimeout(() => {23 if (self.status === PENDING) {24 self.status = REJECTED;25 self.value = reason;26 self.onRejectCallbacks.forEach(cb => cb(self.value));27 }28 });29 }30 try {31 executor(resolve, reject);32 } catch (error) {33 reject(error);34 }35}36function resolvePromise(promise2, x, resolve, reject) {37 if (x === promise2) {38 return reject(new TypeError("循ç¯å¼ç¨"));39 }40 let called = false;41 if (x instanceof Promise2) {42 if (x.status === PENDING) {43 x.then(function(y) {44 resolvePromise(promise2, y, resolve, reject);45 }, reject);46 } else {47 x.then(resolve, reject);48 }49 } else if (x !== null && (typeof x === "object" || typeof x === "function")) {50 try {51 const { then } = x;52 if (typeof then === "function") {53 then.call(54 x,55 function(y) {56 if (called) return;57 called = true;58 resolvePromise(promise2, y, resolve, reject);59 },60 function(reason) {61 if (called) return;62 called = true;63 reject(reason);64 }65 );66 } else {67 resolve(x);68 }69 } catch (error) {70 if (called) return;71 called = true;72 reject(error);73 }74 } else {75 resolve(x);76 }77}78Promise2.prototype.then = function(onFulfilled, onRejected) {79 onFulfilled =80 typeof onFulfilled === "function" ? onFulfilled : value => value;81 onRejected =82 typeof onRejected === "function"83 ? onRejected84 : reason => {85 throw reason;86 };87 const self = this;88 if (self.status === FULFILLED) {89 const promise2 = new Promise2((resolve, reject) => {90 setTimeout(() => {91 try {92 const x = onFulfilled(self.value);93 resolvePromise(promise2, x, resolve, reject);94 } catch (error) {95 reject(error);96 }97 });98 });99 return promise2;100 }101 if (self.status === REJECTED) {102 const promise2 = new Promise2((resolve, reject) => {103 setTimeout(() => {104 try {105 const x = onRejected(self.value);106 resolvePromise(promise2, x, resolve, reject);107 } catch (error) {108 reject(error);109 }110 });111 });112 return promise2;113 }114 if (self.status === PENDING) {115 const promise2 = new Promise2((resolve, reject) => {116 self.onResolveCallbacks.push(function() {117 try {118 const x = onFulfilled(self.value);119 resolvePromise(promise2, x, resolve, reject);120 } catch (error) {121 reject(error);122 }123 });124 self.onRejectCallbacks.push(function() {125 try {126 const x = onRejected(self.value);127 resolvePromise(promise2, x, resolve, reject);128 } catch (error) {129 reject(error);130 }131 });132 });133 return promise2;134 }135};136Promise2.prototype.catch = function(onRejected) {137 this.then(null, onRejected);138};139Promise2.prototype.finally = function(cb) {140 const p = this.constructor;141 return this.then(142 value => p.resolve(cb()).then(() => value),143 reason =>144 p.resolve(cb()).then(() => {145 throw reason;146 })147 );148};149Promise2.deferred = Promise2.defer = function() {150 const defer = {};151 defer.promise = new Promise2(function(resolve, reject) {152 defer.resolve = resolve;153 defer.reject = reject;154 });155 return defer;156};157Promise2.resolve = function(value) {158 if (value instanceof Promise2) return value;159 return new Promise2((resolve, reject) => {160 if (value && value.then && typeof value.then === "function") {161 value.then(resolve, reject);162 } else {163 resolve(value);164 }165 });166};167Promise2.reject = function(reason) {168 return new Promise2((resolve, reject) => {169 reject(reason);170 });171};172function allHelp(len, resolve) {173 let [count, result] = [0, []];174 return function done(value, i) {175 result[i] = value;176 count += 1;177 if (count === len) resolve(result);178 };179}180Promise2.all = function(promises) {181 return new Promise2((resolve, reject) => {182 const next = allHelp(promise.length, resolve);183 for (let i = 0; i < promises.length; i += 1) {184 promises[i].then(value => {185 next(value, i);186 }, reject);187 }188 });189};190Promise2.race = function(promises) {191 return new Promise2((resolve, reject) => {192 if (promise.length === 0) return;193 for (let i = 0; i < promises.length; i += 1) {194 promises[i].then(resolve, reject);195 }196 });197};198Promise2.serialPromise = function(promises) {199 promises.reduce((acc, cur) => {200 return acc.then(() => {201 cur.then(value => value);202 });203 }, Promise.resolve);204};...
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 return page.get();4}).then(function(data) {5 console.log(data);6});7var wptools = require('wptools');8wptools.page('Barack Obama').get(function(err, data) {9 console.log(data);10});11var wptools = require('wptools');12wptools.page('Barack Obama').get(function(err, data) {13 console.log(data);14});15var wptools = require('wptools');16wptools.page('Barack Obama').get(function(err, data) {17 console.log(data);18});19var wptools = require('wptools');20wptools.page('Barack Obama').then(function(page) {21 return page.get();22}).then(function(data) {23 console.log(data);24});25var wptools = require('wptools');26wptools.page('Barack Obama').get(function(err, data) {27 console.log(data);28});29var wptools = require('wptools');30wptools.page('Barack Obama').get(function(err, data) {31 console.log(data);32});33var wptools = require('wptools');34wptools.page('Barack Obama').get(function(err, data) {35 console.log(data);36});37var wptools = require('wptools');38wptools.page('Barack Obama').then(function(page) {39 return page.get();40}).then(function(data) {41 console.log(data);42});43var wptools = require('wptools');44wptools.page('Barack Obama').get(function(err, data) {45 console.log(data);46});47var wptools = require('wptools');48wptools.page('Barack Obama').get(function(err, data)
Using AI Code Generation
1var wp = require('wptoolkit');2.then(function (result) {3 console.log(result);4})5.catch(function (err) {6 console.log(err);7});8.then(function (result) {9 console.log(result);10})11.catch(function (err) {12 console.log(err);13});14 if (err) {15 console.log(err);16 } else {17 console.log(result);18 }19});20 if (err) {21 console.log(err);22 } else {23 console.log(result);24 }25});26 if (err) {27 console.log(err);28 } else {29 console.log(result);30 }31});32 if (err) {33 console.log(err);34 } else {35 console.log(result);36 }37});
Using AI Code Generation
1const wpt = require('webpagetest');2const wptPromise = require('webpagetest-promise');3const wptPromise2 = require('webpagetest-promise2');4const wpt2 = wptPromise2('www.webpagetest.org', 'A.2f0a0f2d2e6a8dc8c0b9b1e1a9a9f3a3');5const wpt3 = wptPromise('www.webpagetest.org', 'A.2f0a0f2d2e6a8dc8c0b9b1e1a9a9f3a3');6 if (err) {7 console.log(err);8 } else {9 console.log(data);10 }11});12 console.log(data);13}).catch(err => {14 console.log(err);15});16 videoParams: {17 }18 }, (err, data) => {19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24 }25);26'use strict';27const request = require('request');28class WPTPromise2 {29 constructor(host, key) {30 this.host = host;31 this.key = key;32 this.options = {33 headers: {34 }35 };36 }37 runTest(url, options, callback) {38 if (typeof options === 'function
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var promise2 = wptoolkit.promise2;3promise2({4}).then(function (result) {5 console.log(result);6}).catch(function (err) {7 console.log(err);8});9wptoolkit.promise({10}).then(function (result) {11 console.log(result);12}).catch(function (err) {13 console.log(err);14});15wptoolkit.callback({16}, function (err, result) {17 if (err) {18 console.log(err);19 } else {20 console.log(result);21 }22});23wptoolkit.callback({24}, function (err, result) {25 if (err) {26 console.log(err);27 } else {28 console.log(result);29 }30});31wptoolkit.promise({32}).then(function (result) {33 console.log(result);34}).catch(function (err) {35 console.log(err);36});37promise2({38}).then(function (result) {39 console.log(result);40}).catch(function (err) {41 console.log(err);42});43wptoolkit.callback({44}, function (err, result) {45 if (err) {46 console.log(err);47 } else {48 console.log(result);49 }50});51wptoolkit.promise({52}).then(function
Using AI Code Generation
1var wptools = require('wptools');2var promise2 = wptools.page('Albert_Einstein').promise2();3promise2.then(function (doc) {4 console.log(doc);5});6var wptools = require('wptools');7var promise = wptools.page('Albert_Einstein').promise();8promise.then(function (doc) {9 console.log(doc);10});11var wptools = require('wptools');12var promise = wptools.page('Albert_Einstein').image().promise();13promise.then(function (doc) {14 console.log(doc);15});16var wptools = require('wptools');17var promise = wptools.page('Albert_Einstein').infobox().promise();18promise.then(function (doc) {19 console.log(doc);20});21var wptools = require('wptools');22var promise = wptools.page('Albert_Einstein').categories().promise();23promise.then(function (doc) {24 console.log(doc);25});26var wptools = require('wptools');27var promise = wptools.page('Albert_Einstein').links().promise();28promise.then(function (doc) {29 console.log(doc);30});31var wptools = require('wptools');32var promise = wptools.page('Albert_Einstein').references().promise();33promise.then(function (doc) {34 console.log(doc);35});36var wptools = require('wptools');37var promise = wptools.page('Albert_Einstein').coordinates().promise();38promise.then(function (doc) {39 console.log(doc);40});41var wptools = require('wptools');42var promise = wptools.page('Albert_Einstein').extract().promise();43promise.then(function (doc) {44 console.log(doc);45});46var wptools = require('wptools');47var promise = wptools.page('Albert_E
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 return page.getWikiText();4}).then(function(wikitext) {5 console.log(wikitext);6});7wptools.page('Barack Obama', function(err, page) {8 page.getWikiText(function(err, wikitext) {9 console.log(wikitext);10 });11});12 throw err;
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var page = wptools.page(url);4var options = {5};6page.get(options, function(err, response) {7 if (err) {8 console.log(err);9 } else {10 console.log(response);11 fs.writeFile('barack.json', JSON.stringify(response), function(err) {12 if (err) {13 console.log(err);14 } else {15 console.log('Saved!');16 }17 });18 }19});20var wptools = require('wptools');21var fs = require('fs');22var page = wptools.page(url);23var options = {24};25page.get(options)26 .then(function(response) {27 console.log(response);28 fs.writeFile('barack.json', JSON.stringify(response), function(err) {29 if (err) {30 console.log(err);31 } else {32 console.log('Saved!');33 }34 });35 })36 .catch(function(err) {37 console.log(err);38 });39var wptools = require('wptools');40var fs = require('fs');41var page = wptools.page(url);42var options = {43};44page.get(options)45 .then(function(response) {46 console.log(response);47 fs.writeFile('barack.json', JSON.stringify(response), function(err) {48 if (err) {49 console.log(err);50 } else {51 console.log('Saved!');52 }53 });54 })55 .catch(function(err) {56 console.log(err);57 });
Using AI Code Generation
1var wiki = require('wikijs').default;2var wptools = require('wptools');3var page = wptools.page('Albert Einstein');4page.get(function(err, resp) {5 console.log(resp);6});7var wiki = require('wikijs').default;8var page = wiki().page('Albert Einstein');9page.then(function(page) {10 page.fullInfo().then(function(info) {11 console.log(info);12 });13});14var wiki = require('wikijs').default;15var wptools = require('wptools');16var page = wptools.page('Albert Einstein');17page.get(function(err, resp) {18 console.log(resp);19});20var wiki = require('wikijs').default;21var page = wiki().page('Albert Einstein');22page.then(function(page) {23 page.fullInfo().then(function(info) {24 console.log(info);25 });26});27var wiki = require('wikijs').default;28var wptools = require('wptools');29var page = wptools.page('Albert Einstein');30page.get(function(err, resp) {31 console.log(resp);32});33var wiki = require('wikijs').default;34var page = wiki().page('Albert Einstein');35page.then(function(page) {
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!!