1

I am trying to write an service to get the list of customers but i don't know to return the list of customers from nested promose.

Please help me on this thanks in advance.

import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';

@Injectable()
export class CustomerService {
    private sDBName:string;
    private db;
    private isDBExist:boolean = false;

    constructor() {}

    setDBName(sDBName:string) {
        this.sDBName = sDBName;
    }

    connect():Promise<any> {        
        this.db = new SQLite();
        return this.db.openDatabase({
            name: this.sDBName,
            location: 'default'
        });
    }
    getCustomersList():Promise<any> {
        return Promise.resolve(()=>{            
            return this.connect().then(()=>{                
                this.isDBExist = true;
                let sql = 'SELECT * FROM customer ORDER BY customer_id DESC LIMIT 10';
                return this.db.executeSql(sql, {}).then((result)=>{ 
                    let customers = [];             
                    for(let i=0; i<result.rows.length; i++) {
                        customers.push(result.rows.item(i));
                    }
                    return customers;
                },(err)=>{
                    this.debug('Unable to select customers', err);
                    return [];
                });
            },(err)=>{
                this.debug('Unable to open database', err);
                return [];
            });
        });
    }
}
4
  • What's the problem? Where and how do you call getCustomerList()? Commented Nov 19, 2016 at 16:58
  • i don't know how to return the customers list getCustomersList() is this code has some issues inside the getCustomersList() i couldn't figure it out Commented Nov 19, 2016 at 17:00
  • return Promise.resolve(()=>{ ... }) resolves with a function, it doesn't call it. Is there a reason why it shouldn't be return this.connect()...? Commented Nov 19, 2016 at 20:55
  • return statement is not waiting to resolve the promise. It's returns empty. How to make return statement wait Commented Nov 20, 2016 at 5:03

1 Answer 1

3

You need to chain the call with .then(...) like

this.getCustomerList().then(result => this.myArray = result);
Sign up to request clarification or add additional context in comments.

3 Comments

can you clarify me only one thing is the getCustomerList() function is right
angular 2 return statement will wait for the promise to be completed
this question is related to this only stackoverflow.com/questions/37841721/… .

Your Answer

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.