We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
Yo, is there a way to get a hint on this platform?
hmm.. I cant solve practice in #9.
The output was
Testing with data test (string)
Testing with data 3 (number)
Testing with data 6 (number)
Promise is resolved but it should be rejected
but basically I've already using isNaN function to determine if is not a number.
if(isNaN(data) === false){
doing check if data is even or odd, then use settimeout
}else{
call function from second parameter which use for handle error
}
I struggled on that one too. But found out that we need to reject the promise if the number is even or not a number! Question was simple though!
`
return new Promise((resolve, reject) => {
if(isNaN(data)){
reject('error');
} else if(data%2 === 0) {
setTimeout(reject, 2000, 'even');
} else {
setTimeout(resolve, 1000, 'odd');
}
});
`
This is my answer:
function job(data) {
return new Promise(function(resolve, reject){
if(isNaN(data)){
reject("error");
}else if(data%2!=0){
setTimeout(function(){
resolve("odd");
},1000)
}else if(data%2==0){
setTimeout(function(){
reject("even");
},2000)
}
})
}
forget it, I didnt notice the rules.. the odd should be return as rejected
Stuck on 8/17...function job() {
return new Promise((resolve) => {
setTimeout(() => resolve('hello word'), 2000);
})
}
if you still need it (or someone else): function(resolve,reject) instead of function(resolve)
whats the point? resolve is never called
I guess we all know what green and red indicate, but for a first-time user on the first click of the button, there is a lingering question of "does the button turning green mean I got it right, or am I still waiting for something more to happen". Might be nice if the text of the button changed, too. "Got it!" or "Try again" might be appropriate.
What's the answer of 8 ? can anyone guide me with that?
Why do we call databases like functions? Why do we do something like central(id), but not central.id, or central[id].db? I don't really understand how we navigate through those databases and get the user information, and how does central(id) even work if it's not a function?
#9 SUCCESS:
function job(data) {
return new Promise(function(resolve,reject){
if(isNaN(data)){
reject("error");
}else if(data%2 != 0){
setTimeout(function(){
resolve("odd")
},1000)
}else{
setTimeout(function(){
reject("even")
},2000)
}
})
}
Typo: "Some pratice" should be "Some practice"
Fixed. Thanks. As well as others typos.
The code sections should be expandable.
What do you mean by expandable? Normally the code section is big enough to contain the whole code. If not, what's your browser?
It seems like it was a bug yesterday. I contacted the tech.io helpdesk and they said they have fixed it now. :)
#9 Success
function job(data) {
return new Promise((resolve,reject) => {
if (isNaN(data)) reject("error");
(!isNaN(data) && data % 2 != 0) ? setTimeout(() => resolve("odd"), 1000)
: setTimeout(() => reject("even"), 2000);
});
}
Typo:
"In the future, synchronous http requests will be not supported by browsers"
should be
"In the future, synchronous http requests will not be supported by browsers"
Two ways:
1st: Using Callbackhell:
function job(callback1, callback2) {
setTimeout(() => callback1(), 2000)
function job1(callback2) {
setTimeout(() => {
callback2();
setTimeout(() => {
callback2();
setTimeout(() => {
callback2()
}, 1000)
}, 1000)
}, 1000)
}
job1(callback2);
}
2nd: Using setInterval
function job(callback1, callback2) {
setTimeout(() => callback1(), 2000)
let counter =0;
const timer = setInterval(() => {
callback2();
counter+=1;
if (counter >= 3) {
clearInterval(timer)
}
},1000)
}
function job(callback1, callback2) {
setTimeout(() => callback1(), 2000)
let counter =0;
const timer = setInterval(() => {
callback2();
counter+=1;
if (counter >= 3) {
clearInterval(timer)
}
},1000)
}
#4 My solution
function job(callback1, callback2) {
setTimeout(function(){
callback1();
}, 2000);
var interval = setInterval(function(){
callback2();
}, 1000);
setTimeout(function(){
clearInterval(interval);
}, 3500)
}
module.exports = job;
#9
function job(data) {
return new Promise((resolve, reject) => {
isNaN(data)
? reject("error")
: (data % 2) === 0
? setTimeout(() => reject("even"), 2000)
: setTimeout(() => resolve("odd"), 1000)
});
}
module.exports = job;
Here is my answer :
function job() {
return new Promise((resolve, reject) =>{
setTimeout(() =>{
resolve('hello world')
} , 2000)
})
}
module.exports = job;
#8 Success
function job() {
return new Promise((resolve,reject)=>{
setTimeout(function(){resolve("hello world")},2000)
});
}
#9 Problem, successfully answered. Using ternary operator.
function job(data) {
return new Promise((resolve, reject) => {
if(isNaN(data)){ reject("error") }
else {
data % 2 === 0 ? setTimeout(() => reject("even"), 2000) : setTimeout(() => resolve("odd"), 1000)
}
});
}
module.exports = job;
function job() {
setTimeout(() => {
let promise = new Promise((resolve, reject)=>{
return resolve("hello world")
})
promise.then((data)=> console.log(data))
}, 2000)
}
module.exports = job;
result falsy
Really love this post! Thanks for sharing good points of the pitfalls regarding promises.
Regarding the pyramid of doom, i do not get the "You broke the chain again."
Consider the following code:
function sleep(milis) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("resolving promise with ", milis, " sleep")
resolve(true);
}, milis);
})
}
function test_chain_pyramid() {
return sleep(1000).then(function() {
return sleep(2000).then(function() {
return sleep(3000).then(function() {
return sleep(4000).then(function() {
return 4
});
});
});
});
}
function test_chaned_nicely() {
return sleep(1000).then(function() {
return sleep(2000);
}).then(function() {
return sleep(3000);
}).then(function() {
return sleep(4000);
}).then(function() {
return 4
});
}
The results of test_chaned_nicely() and test_chain_pyramid() executions are exactly the sames and the promises execution order is respected.
So i keep wonder what chain do i broke.
THe formatting is awful, please bear with me and ident the code using a proper ide or program. Nevertheless the code is the same and the question is sustained.
Can someone please explain what is wrong with my code?
Testing with data test (string)
Testing with data 3 (number)
Promise is rejected but it should be resolved
function job(data) {
return new Promise((resolve, reject) => {
let x = data % 2;
if(isNaN(data)) {
reject('error');
} else if(x !== 0) {
setTimmeout(() => resolve('odd'), 1000);
} else {
setTimeout(() => reject('even'), 2000);
}});
}
you have a typo, "setTimmeout"
Promise.all with catch
Why would you do this instead just use allSettled and then foreach data.value
IMHO this breaks the intention behind the .all which is to resolve all or shortcircuit when rejected.
Hi, I am not able to solve #8, here is my code
const job = new Promise((resolve, reject) => {
setTimeout(()=>{
resolve('hello world')
}, 2000)
})
job.then((data)=>{
console.log(data)
})
module.exports = job;
___
I am getting this error -
/project/target/referee.js:2
promise = require('./code.js')(),
^
TypeError: require(...) is not a function
at Object.<anonymous> (/project/target/referee.js:2:35)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
Can anyone please help me in this? Thanks in advance
Job should be a function that returns a promise?
Thats my guess.
Mine is:
[1, 2, 3].reduce((promise, count) =>
promise.then(() =>
new Promise(res =>
setTimeout(() => {
callback2();
if (count === 2) callback1();
res();
}, 1000))
), Promise.resolve());
The last challenge is quite simply impossible for me. I can't figure it out for the life of me. Obviously without looking at the solution.
#8 SUCCESS:
function job() {
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve('hello world')
}, 2000)
});
}
Hello, can anyone explain why my solution doesn't work for #16, the last challenge?
module.exports = function(id) {
return new Promise(function(resolve, reject){
Promise.all([
central(id)
.catch(function(e) {
return Promise.reject("Error central");
})
.then(function(db) {
if (db === "db1") {
return db1(id).catch((e)=> {
return Promise.reject(`Error ${db}`);
});
}
if (db === "db2") {
return db2(id).catch((e)=> {
return Promise.reject(`Error ${db}`);
});
}
if (db === "db3") {
return db3(id).catch((e)=> {
return Promise.reject(`Error ${db}`);
});
}
}),
vault(id)
.catch(function(e){
return Promise.reject("Error vault");
})
])
.then(function(data){
mark(id).catch((e) => {});
const user = {id: id, username: data[0].username, country: data[0].country, firstname: data[1].firstname, lastname: data[2].lastname, email: data[1].email};
console.log(user);
resolve(user);
})
.catch(function(e){
reject(e);
});
});
};
Unfortunately the following quoted fragment is false. The setTimeout and setInterval functions aren't native. Not more so than fs.readFile or document.exitFullscreen.
function job(callback1, callback2) {
setTimeout(()=> callback1(), 2000);
let f2 = setInterval(()=>callback2(), 1000);
setTimeout(()=> clearInterval(f2), 4000)
}
module.exports = job;
function job(callback1, callback2) {
setTimeout(callback1, 2000);
let counter = 0;
let timer = setInterval(() => {
callback2();
counter++;
if (counter >=3) {
clearInterval(timer);
}
}, 1000);
}
module.exports = job;
Hi all!
Is the answer for practise #4 the following?
Thanks!
function job(callback1, callback2) {
setTimeout(callback1, 2000);
setInterval(callback2, 1000);
setInterval(callback2, 1000);
setInterval(callback2, 1000);
clearInterval(callback2);
}
module.exports = job;
function job(callback1, callback2) {
setTimeout(function(){
callback1();
},2000
);
let counter = 1;
var timer=setInterval(function(){
callback2();
counter++;
if(counter>3){
clearInterval(timer)
}
},1000)
}
module.exports = job;
Typo in the 14/17 second example.
is: cconsole.log(data);
should be: console.log(data);
PLEASE HELP IN REFACTORING THIS
this.bankClient.checkDetails(payment.beneficiary).then((_beneficiaryResult) => {
this.bankClient.checkDetails(payment.payee).then((_payeeResult) => {
this.fraudManagementClient.checkFraud(payment).then(async (_fraudResult) => {
await this.bankDetails.save(payment.beneficiary);
await this.bankDetails.save(payment.payee);
let savedPayment = await this.payments.save(payment)
return resolve(savedPayment);
});
}).catch((_error) => {
return reject(new BankAccountDetailsNotFound(payment.payee.name + "'s bank account details are invalid"))
})
}).catch((_error) => {
return reject(new BankAccountDetailsNotFound(payment.beneficiary.name + "'s bank account details are invalid"))
})
I'm having trouble understanding the third question of the quiz on page 13/17. When we get to this
.then(function(data) {
console.log(data);
return new Error('test');
})
why does it continue to the next .then statement instead of to the next .catch? Thanks!
By default using just new Error("error") won't throw any error from the program..instead it will continue to the next line.
if you wanted to bring your error into catch block then you need to do something like throw new Error("error")'; (now you will notice it will trigger catch block instead of then block)
.then(function(data) {
console.log(data);
throw new Error('test');
})
So, to not broke the chain, we could also write it like this
function test() {
const promise = job();
const result = promise.then(function(data) {
doSomething(data); // whether doSomething() returns a promise (async) or not (sync), in this example it depends on the data, so this example literally tells us that doSomething() must happen after job()
});
return result;
}
Thanks a lot, it easy to follow it
Wow, I love the "Your first code with promises" page. This has actually helped me a lot as I was actually recently thinking of delving into promises and that stuff. Tech.io is really as good as they say it is!