我想使用promises,但我有一个回调API,格式如下:
window.onload; // set to callback
...
window.onload = function() {
};
Run Code Online (Sandbox Code Playgroud)
function request(onChangeHandler) {
...
}
request(function() {
// change happened
...
});
Run Code Online (Sandbox Code Playgroud)
function getStuff(dat, callback) {
...
}
getStuff("dataParam", function(err, data) {
...
})
Run Code Online (Sandbox Code Playgroud)
API;
API.one(function(err, data) {
API.two(function(err, data2) {
API.three(function(err, data3) {
...
});
});
});
Run Code Online (Sandbox Code Playgroud)
假设我有以下代码.
function divide(numerator, denominator) {
return new Promise((resolve, reject) => {
if(denominator === 0){
reject("Cannot divide by 0");
return; //superfluous?
}
resolve(numerator / denominator);
});
}
Run Code Online (Sandbox Code Playgroud)
如果我的目的是reject尽早退出,那么我是否应该return立即养成习惯?
我试图从page.evaluate()我使用 Puppeteer 构建的 YouTube 抓取工具的内部获取值。我无法从page.evaluate(). 我如何实现这一目标?这是代码:
let boxes2 = []
const getData = async() => {
return await page.evaluate(async () => { // scroll till there's no more room to scroll or you get at least 250 boxes
console.log(await new Promise(resolve => {
var scrolledHeight = 0
var distance = 100
var timer = setInterval(() => {
boxes = document.querySelectorAll("div.style-scope.ytd-item-section-renderer#contents > ytd-video-renderer > div.style-scope.ytd-video-renderer#dismissable")
console.log(`${boxes.length} boxes`)
var scrollHeight = document.documentElement.scrollHeight
window.scrollBy(0, distance)
scrolledHeight += distance
if(scrolledHeight …Run Code Online (Sandbox Code Playgroud) 如果无法从中找到属性,是否有方法抛出错误data。
问题是,它是映射undefined而不是抛出错误。
const insertIntoTable = function(data) {
return new Promise((resolve, reject) => {
const entry = {
Id: data.id,
Method: data.Method,
Status: data.PaymentStatus,
InsertedAt: (new Date().getTime())
}
}).catch((error) => {
console.log(error);
});
}
Run Code Online (Sandbox Code Playgroud)