Why getUserMedia stopped working suddenly - javascript

I used to use this function to check the mic:
function checkMicrophone() {
console.log('checkMicrophone executed')
return new Promise((resolve, reject) => {
navigator.mediaDevices.getUserMedia({
audio: true
}).then(stream => {
console.log('res')
resolve();
}).catch(err => {
console.log('err')
resolve();
});
});
}
checkMicrophone()
And it was working till just today!
What happened it doesn't return the promise nor the err ?

Related

How do i write promises in javascript

im trying to write a promise but seems to be missing something. here is my code:
const myPromise = new Promise(() => {
setTimeout(() => {
console.log("getting here");
return setinputs({ ...inputs, images: imageAsUrl });
}, 100);
});
myPromise
.then(() => {
console.log("getting here too");
firebase.database().ref(`collection/${idNode}`).set(inputs);
})
.then(() => {
console.log("all is set");
})
.catch((err) => {
console.log(err);
});
if i run the program, the first part of the promise is executing but all .then() functions arent executing. how do i fix this?
In this scheme, the promise callback has one (resolve) or two (resolve,reject) arguments.
let p = new Promise((resolve, reject)=> {
//do something
//resolve the promise:
if (result === "ok") {
resolve(3);
}
else {
reject("Something is wrong");
}
});
p.then(res => {
console.log(res); // "3"
}).catch(err => {
console.error(err); //"Something is wrrong
});
Of course, nowadays you can use async + await in a lot of cases.
You need to resolve the promise, using resolve() and also return the promise from firebase so the next .then in the chain works properly.
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
console.log("getting here");
// You have to call resolve for all `.then` methods to be triggered
resolve({ ...inputs, images: imageAsUrl });
}, 100);
});
myPromise
.then((inputs) => {
console.log("getting here too");
// You have to return a promise in a .then function for the next .then to work properly
return firebase.database().ref(`collection/${idNode}`).set(inputs);
})
.then(() => {
console.log("all is set");
})
.catch((err) => {
console.log(err);
});

Puppeteer-cluster close browsers itself

I use puppeteer-cluster + node js. I have some script.
I'm a newbie
The script runs halfway through and then ends and is not executed further. I've been looking for a solution for a week now, I don't understand what's the matter. Help me please enter code here
const { Cluster } = require('puppeteer-cluster-delay');
(async () => {
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 2,
puppeteerOptions: {headless: false,
},
});
await cluster.task(async ({ page, data: url }) => {
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('1');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('2');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('3');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('4');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('5');
await page.goto('http://www.nike.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('6');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('7');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('8');
await page.goto('http://www.google.com/');
await new Promise(resolve => {
setTimeout(() => { // user is waiting for an asynchronous event
try {
resolve();
} catch (err) {
// handle error
}
}, 5000);
});
await console.log('end');
});
await cluster.queue();
await cluster.queue();
// many more pages
await cluster.idle();
await cluster.close();
})();
the script reaches the 5th iteration and ends ((
We need to use timeout argument in launching puppeteer.
Set timeout = 0 to off ones

Rejecting a Promise with another new Promise

Im playing around with promises and i wanted a way of rejecting the promise inside of a then callback. So this is done by either calling throw return or return Promise.reject();. So far so good. You can accomplish this by also calling new Promise.reject(); withou a return.
Can someone explain why does this work?
new Promise((res, rej) => {
setTimeout(() => {
res(200);
}, 2000);
})
.then(() => {
console.log("ok1");
new Promise.reject();
// return Promise.reject();
.then(() => {
console.log("ok2");
})
.catch(() => {
console.log("error");
});
new Promise.reject()
Your code throws an exception. Exceptions cause promises to be rejected.
Because Promise.reject is not a constructor, this code works fine:
new Promise((res, rej) => {
setTimeout(() => {
res(200);
}, 2000);
})
.then(() => {
console.log('ok1')
Promise.reject('error!')
})
.then(() => {
console.log("ok2");
})
.catch(() => {
console.log("error");
});
New Promise.reject throws an error so the code jumps to the catch section with the following error : Promise.reject is not a constructor
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('OK');
}, 2000);
}).then((res) => {
console.log(res);
new Promise.reject();
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log('error' ,err.message);
});

break from promise mapseries

I have an array of methods and I running them by Promise.mapSeries:
Promise.mapSeries(functions.map(function => {
return new Promise((resolve, reject) => {
function(req, res, function(err) {
if (success) {
return something(); //if we here, break mapSeries
}
resolve();
});
})
}))
.then(() => {
return something();
})
How I can break from mapSeries, to prevent execution rest of functions?
Since you are returning something() you won't execute resolve(); so maybe try to resolve the promise before?
Promise.mapSeries(functions.map(function => {
return new Promise((resolve, reject) => {
function(req, res, function(err) {
if (success) {
return Promise.resolve(something()); //if we here, break mapSeries
}
});
})
}))
.then(() => {
return something();
})

How do I choose the best URL to fetch?

I'm looking for the best URL (say a local cache) at a set of URLs: ['https://1.2.3.4', 'https://httpbin.org/delay/3', 'https://httpbin.org/status/500'] and choose the best working one with a 5 second timeout. Else fallback to 'https://httpbin.org/status/200'. In this contrived example, 'https://httpbin.org/delay/3' should win!
function fallback(response) {
if (response.ok) {
return response;
}
console.log("Trying fallback")
return fetch("https://httpstat.us/200") // absolute fallback
}
var p = Promise.race([
fetch('https://1.2.3.4'), // will fail
fetch('https://httpbin.org/delay/3'), // should be the winner
fetch('https://httpbin.org/status/500'), // will fail
new Promise(function(resolve, reject) { // Competing with a timeout
setTimeout(() => reject(new Error('request timeout')), 5000)
})
])
.then(fallback, fallback)
.then(console.log)
The code I have has the problem whereby the Promise race ends when any returns. How can I best construct this so that 'https://httpbin.org/delay/3' will correctly win the race?
Promise.race([
new Promise(resolve => {
const ignoreError = () => {}
// first to resolve with .ok gets to resolve the promise
const resolveIfOk = res => res.ok && resolve(res)
fetch('https://httpbin.org/delay/20').then(resolveIfOk, ignoreError)
fetch('https://httpbin.org/delay/3').then(resolveIfOk, ignoreError)
fetch('https://1.2.3.4').then(resolveIfOk, ignoreError)
return;
}),
new Promise((_, reject) => setTimeout(() => reject(new Error('timed out')), 4000)) // reject if timeout
]).then(function(value) {
console.log("Success", value);
return value
}, function(reason) {
console.log("Going to use fallback", reason);
return fetch('https://httpstat.us/200')
}).then(console.log)
The solution comes via https://twitter.com/secoif
function race(promises) {
return Promise.race(Object.keys(promises).map((key) => {
return promises[key]
}))
.then(function(response) {
console.log(response);
if (!response.ok) {
delete promises[response.url];
return race(promises)
} else if (Object.keys(promises).length == 0 || response.url == 'timeout') {
console.log('returning default');
return fetch("https://httpstat.us/200")
}
return response;
})
}
race({
'https://1.2.3.4': fetch('https://1.2.3.4'),
'https://1.2.3.4': fetch('https://1.2.3.4'),
'timeout': new Promise(function(resolve, reject) {
setTimeout(() => reject(new Error('request timeout')), 5000)
})
}).then(function(response) {
console.log(response)
}).catch(function(err) {
console.log('err:');
console.log(err.message)
return fetch("https://httpstat.us/200")
}).then(function(defaultFetch) {
console.log('timed out requesting failed urls, getting default:');
console.log(defaultFetch);
});

Categories

Resources