Quick actions

cmd+k|ctrl+k

Navigation

Languages

How to chain func with using Promise.Then?

Snippet info

Language

JavaScript

Visibility

public

Author

myles

Created

2018-01-12T18:19:01Z

Updated

2018-01-12T18:20:48Z

let Query = async function( method, endpoint, options, successCode, obKey ){

    //return true;
    return new Error( 'Could not complete query!' );
    
};

let isAlive = async function( options ){
	try {
		return await Query( 'GET', '/heart', options, 204 );
	} catch( error ){
		return error;
	}
};

let getNetworks = async function(options) {
	try {
		return await Query( 'GET', '/networks', options, 200, 'networks' );
	} catch( error ){
		return error;
	}
};

// Standard promise method works
isAlive().then( () => {
		getNetworks().then( result => {
			console.log( 'GET NETWORKS', result );
		}).catch( error => {
			console.log( 'GET NETWORKS ERROR', error.message );
		});
	}
);

// BUT to make for cleaner code base, how can I only call next function in chain
// based on isAlive() function?

// isAlive().getNetworks().then( result => {
// 	console.log( 'GET HOMIE NETWORKS', result );
// }).catch( error => {
// 	console.log( 'GET HOMIE NETWORKS ERROR', error.message );
// });
INFO