Two New York City police officers were shot and killed as they sat in their patrol car by an attacker thought to have posted anti-police threats on social media.