1

I am new to koa.js and liked it very much, started a project with it. i need to use twilio for sms sending.

most calls for twilio package follow this structure.

public.get('/najam', function *(){
 this.body = "hello from najam";
 //yeild before c.sendSms or inside callback?
 c.sendSms({
 to:'YOUR_PHONE',
 }, function(e, m) {
 if (!e) {
 //yeild here?
 } 
 });
}); 

how can i modify it to put it inside generator function and at what point i will use yield keyword?

If your answer suggesting to use Co library please provide me example with code and bit explanation.

John Slegers
47.4k23 gold badges205 silver badges173 bronze badges
asked Feb 10, 2016 at 13:48

2 Answers 2

2

I just found out how to do this without promises. It's pretty cool, with minimum code and without external libraries. Credit goes to: Josef Sima

function sendSmsAsync(phone) {
 return function(callback) {
 c.sendSms({to: phone}, callback);
 }
}
yield sendSmsAsync("5551212");

This little snippet converts any function that uses node callbacks to a generator. You can wrap sendSmsAsync call in try..catch too. And don't forget to set --harmony option in node.

answered Jun 17, 2016 at 3:43
Sign up to request clarification or add additional context in comments.

1 Comment

How do you indicate an error? Promise lets you use reject() for that.
1

Just wrap callback-based interfaces with a Promise so that you can yield it in the route.

function sendSms(toPhone, textMessage) {
 return new Promise(function(resolve, reject) {
 c.sendSms({ to: toPhone, message: textMessage }, function(err, result) {
 if (err) return reject(err);
 resolve(result);
 });
 });
}

Now you can yield it inside of the route. If it throws an error (like if the network is down), then Koa's default error handler will catch it and turn it into a 500 error.

public.get('/najam', function *(){
 this.body = "hello from najam";
 yield sendSms('YOUR_PHONE', 'SOME_MESSAGE');
}); 

Or you can try/catch it yourself if you want to handle the error in some specific way:

public.get('/najam', function *(){
 this.body = "hello from najam";
 var result;
 try {
 result = yield sendSms('YOUR_PHONE', 'SOME_MESSAGE');
 } catch(err) {
 // Maybe we just wanna log the error to a server before rethrowing
 // it so Koa can handle it
 logError(err);
 throw err;
 }
}); 

When wrapping something with a Promise, it's just a matter of calling reject(err) when there's an error and resolve(result) when it successfully completes.

John Slegers
47.4k23 gold badges205 silver badges173 bronze badges
answered Feb 10, 2016 at 21:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.