Callback Functions 2

Video

JavaScript Notes

JavaScript
    const app = {
        geolocation: (good, bad) = {
            // requires two functions to be passed in 
            try{
                // try to do something
                let pos = {
                latitude:0, longitude: 0
                }
                pos.latitude = (Math.random( ) * 360) - 180; 
                pos.longitude = (Math.random( ) * 180) - 90; 
                if(Math.round(Math.random( )) === 1) {
                    throw new Error("my simulated geolocation failure")
                } else {
                    good.call(app.geolocation, pos);
                }
            } catch(err) { 
                // if it fails we will call the bad function
                // and pass along the error object 
                bad.call(app.geolocation, err);
            }
        }
    }

    let success = (position) => {
        console.log('You are "at" position (${position.latitude}, ${position.longitude})')
    }

    let fail = (err) => {
        console.warn('Something bad happened', err.message)
    }

    app.geolocation(success, fail);	// these are names of functions rather than plain variables