We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Ethan • 6 years ago

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!

tripdog • 2 years ago

Yo, is there a way to get a hint on this platform?

GrennKren • 5 years ago

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
}

Ashwatthama • 4 years ago

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');
}

});
`

Chen • 2 years ago

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)
}
})
}

GrennKren • 5 years ago

forget it, I didnt notice the rules.. the odd should be return as rejected

Grim22 • 2 years ago

Stuck on 8/17...
function job() {
return new Promise((resolve) => {
setTimeout(() => resolve('hello word'), 2000);
})
}

Anonymous • 2 years ago

if you still need it (or someone else): function(resolve,reject) instead of function(resolve)

Anonymous • 2 years ago

whats the point? resolve is never called

kaos2oak • 6 years ago

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.

Stream Debate • 1 year ago

What's the answer of 8 ? can anyone guide me with that?

kywik • 1 year ago

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?

Elo33 • 2 years ago

#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)
}
})
}

kaos2oak • 6 years ago

Typo: "Some pratice" should be "Some practice"

Magus • 6 years ago

Fixed. Thanks. As well as others typos.

Jason Lukose • 6 years ago

The code sections should be expandable.

Jerome Cance • 6 years ago

What do you mean by expandable? Normally the code section is big enough to contain the whole code. If not, what's your browser?

Anonymous • 6 years ago

It seems like it was a bug yesterday. I contacted the tech.io helpdesk and they said they have fixed it now. :)

Abilal • 1 year ago

#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);
});
}

kaos2oak • 6 years ago

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"

Pooja Hegde • 1 year ago

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)
}

Pooja Hegde • 1 year ago

function job(callback1, callback2) {

setTimeout(() => callback1(), 2000)

let counter =0;
const timer = setInterval(() => {
callback2();

counter+=1;

if (counter >= 3) {
clearInterval(timer)
}
},1000)
}

Bruno Gabado • 1 year ago

#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;

wztd • 1 year ago

#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;
KevinMac • 1 year ago

Here is my answer :
function job() {

return new Promise((resolve, reject) =>{
setTimeout(() =>{
resolve('hello world')
} , 2000)
})
}

module.exports = job;

_Sanat • 1 year ago

#8 Success
function job() {
return new Promise((resolve,reject)=>{
setTimeout(function(){resolve("hello world")},2000)
});
}

Zero_One_532 • 1 year ago

#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;

seekinfox • 2 years ago


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

Lord Vituchon • 2 years ago

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.

Lord Vituchon • 2 years ago

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.

Otterspace • 2 years ago

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);
}});


}


Anonymous • 2 years ago

you have a typo, "setTimmeout"

Dante Kovač • 2 years ago

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.

Anonymous • 2 years ago

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

Anonymous • 2 years ago

Job should be a function that returns a promise?
Thats my guess.

olenaag • 2 years ago

Mine is:

[1, 2, 3].reduce((promise, count) =>
promise.then(() =>
new Promise(res =>
setTimeout(() => {
callback2();
if (count === 2) callback1();
res();
}, 1000))
), Promise.resolve());

burgerman • 2 years ago

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.

Elo33 • 2 years ago

#8 SUCCESS:

function job() {

return new Promise(function(resolve,reject){
setTimeout(function(){
resolve('hello world')
}, 2000)
});
}

Matt • 2 years ago

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.

Anonymous • 3 years ago

function job(callback1, callback2) {

setTimeout(()=> callback1(), 2000);

let f2 = setInterval(()=>callback2(), 1000);

setTimeout(()=> clearInterval(f2), 4000)

}

module.exports = job;

chris wang • 3 years ago

function job(callback1, callback2) {
setTimeout(callback1, 2000);
let counter = 0;
let timer = setInterval(() => {
callback2();
counter++;
if (counter >=3) {
clearInterval(timer);
}
}, 1000);
}
module.exports = job;

Anonymous • 3 years ago

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;

Sarah Bao • 4 years ago

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;

paszo • 4 years ago

Typo in the 14/17 second example.
is: cconsole.log(data);
should be: console.log(data);

shyamkondisetty • 4 years ago

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"))
})

Agata • 4 years ago

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!

mohammed • 4 years ago

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');
})

Man Saha • 4 years ago

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;
}

Fezekile Plaatyi • 4 years ago

Thanks a lot, it easy to follow it